springboot如何实现自动装配源码解读
SpringBoot自动装配
最重要的注解@SpringBootApplication
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters={@Filter(type=FilterType.CUSTOM,classes=TypeExcludeFilter.class), @Filter(type=FilterType.CUSTOM,classes=AutoConfigurationExcludeFilter.class)}) public@interfaceSpringBootApplication{ @AliasFor(annotation=EnableAutoConfiguration.class) Class>[]exclude()default{}; @AliasFor(annotation=EnableAutoConfiguration.class) String[]excludeName()default{}; @AliasFor(annotation=ComponentScan.class,attribute="basePackages") String[]scanBasePackages()default{}; @AliasFor(annotation=ComponentScan.class,attribute="basePackageClasses") Class>[]scanBasePackageClasses()default{}; @AliasFor(annotation=Configuration.class) booleanproxyBeanMethods()defaulttrue; }
EnableAutoConfiguration
是实现自动装配的核心注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public@interfaceEnableAutoConfiguration{ }
getAutoConfigurationMetadata()
@Override publicvoidprocess(AnnotationMetadataannotationMetadata,DeferredImportSelectordeferredImportSelector){ Assert.state(deferredImportSelectorinstanceofAutoConfigurationImportSelector, ()->String.format("Only%simplementationsaresupported,got%s", AutoConfigurationImportSelector.class.getSimpleName(), deferredImportSelector.getClass().getName())); AutoConfigurationEntryautoConfigurationEntry=((AutoConfigurationImportSelector)deferredImportSelector) .getAutoConfigurationEntry( //加载配置元数据 getAutoConfigurationMetadata(),annotationMetadata); this.autoConfigurationEntries.add(autoConfigurationEntry); for(StringimportClassName:autoConfigurationEntry.getConfigurations()){ this.entries.putIfAbsent(importClassName,annotationMetadata); } } privateAutoConfigurationMetadatagetAutoConfigurationMetadata(){ if(this.autoConfigurationMetadata==null){ //加载配置信息 this.autoConfigurationMetadata=AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader); } returnthis.autoConfigurationMetadata; } staticAutoConfigurationMetadataloadMetadata(ClassLoaderclassLoader,Stringpath){ try{ //获取资源路径 Enumerationurls=(classLoader!=null)?classLoader.getResources(path) :ClassLoader.getSystemResources(path); Propertiesproperties=newProperties(); while(urls.hasMoreElements()){ properties.putAll(PropertiesLoaderUtils.loadProperties(newUrlResource(urls.nextElement()))); } returnloadMetadata(properties); } catch(IOExceptionex){ thrownewIllegalArgumentException("Unabletoload@ConditionalOnClasslocation["+path+"]",ex); } } protectedstaticfinalStringPATH="META-INF/spring-autoconfigure-metadata.properties";
注意:这个文件在target编译后的文件夹中
自动装配
spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories
该文件内存有:
AutoConfigure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\ org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
EnableConfigurationProperties
@ConfigurationProperties(prefix="spring.redis") publicclassRedisProperties{ /** *Databaseindexusedbytheconnectionfactory. */ privateintdatabase=0; /** *ConnectionURL.Overrideshost,port,andpassword.Userisignored.Example: *redis://user:password@example.com:6379 */ privateStringurl; /** *Redisserverhost. */ privateStringhost="localhost"; /** *Loginpasswordoftheredisserver. */ privateStringpassword; /** *Redisserverport. */ privateintport=6379; /** *WhethertoenableSSLsupport. */ privatebooleanssl; /** *Connectiontimeout. */ privateDurationtimeout; /** *ClientnametobesetonconnectionswithCLIENTSETNAME. */ privateStringclientName; }
希望通过本篇对于springboot自动装配的解读,让大家对springboot底层有了一个大致了解,只分析了主要方法,希望对大家有帮助
到此这篇关于springboot如何实现自动装配源码赏析的文章就介绍到这了,更多相关springboot自动装配内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!