承接前文springboot情操陶冶-@Configuration注解解析,本文將在前文的基礎上對@SpringBootApplication注解作下簡單的分析
@SpringBootApplication
該注解是springboot最集中的一個注解,也是應用最廣泛的注解。官方也多用此注解以啟動spring服務,我們看下其中的源碼
@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 @interface SpringBootApplication {
@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 {};
}
通過上述代碼可知,其整合了@EnableAutoConfiguration
和@ComponentScan
兩個主要的注解,並以此作了默認的指定。
屬性exclude和excludeName,其默認為空,指定的話最終由EnableAutoConfiguration.class來完成解析
屬性scanBasePackages和scanBasePackageClasses,其默認為空,指定的話最終由ComponentScan.class來完成解析
默認情況下,以
@SpringBootApplication
注解的所在類的包名作為beanDefinition掃描路徑。
掃描過程中屏蔽META-INF\spring.factories文件中org.springframework.boot.autoconfigure.EnableAutoConfiguration
屬性指定的class集合支持多個
@ComponentScan
注解同時與@SpringBootApplication
注解搭配使用
@ComponentScan注解在前文中已了解的差不多了,筆者此處就針對@EnableAutoConfiguration注解作下詳細的分析
@EnableAutoConfiguration
單純從英文單詞上看是開啟自動注入的意思,具體的話筆者還是根據源碼來解讀。首先看下注解的源碼
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
Class<?>[] exclude() default {};
/**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
String[] excludeName() default {};
}
它用到了我們前文提及的@Import注解,所以我們關注所引入的AutoConfigurationImportSelector.class
AutoConfigurationImportSelector
其是DeferredImportSelector.class接口的實現類,此處我們直接查看復寫的方法selectImports()
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
// spring.boot.enableautoconfiguration屬性讀取,默認為true
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
// read all META-INF\spring-autoconfigure-metadata.properties files
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader);
// 獲取相應類上@EnableAutoConfiguration對應的屬性
AnnotationAttributes attributes = getAttributes(annotationMetadata);
// read all <org.springframework.boot.autoconfigure.EnableAutoConfiguration> properties from META-INF\spring.factories
List<String> configurations = getCandidateConfigurations(annotationMetadata,
attributes);
configurations = removeDuplicates(configurations);
// read exclude/excludeName property or spring.autoconfigure.exclude property
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
// make sure the assignable exclusions are present in classpath and in configurations collection, or will throw exception
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
// filter the present configurations
configurations = filter(configurations, autoConfigurationMetadata);
// fire the AutoConfigurationImportEvent by AutoConfigurationImportListener.class
fireAutoConfigurationImportEvents(configurations, exclusions);
return StringUtils.toStringArray(configurations);
}
對上述的步驟此處再作下總結
優先判斷環境變量spring.boot.enableautoconfiguration,如果指定為false,則不引入任何類。默認為true
讀取classpath環境下所有的META-INF\spring-autoconfigure-metadata.properties文件,加載其中的所有屬性保存至autoConfigurationMetadata
獲取相應類上@EnableAutoConfiguration對應的屬性,其實也就是exclude屬性和excludeName屬性
讀取classpath環境下所有的META-INF\spring.factories文件中的
org.springframework.boot.autoconfigure.EnableAutoConfiguration
屬性,得到configurations集合根據第三步讀取的配置以及
spring.autoconfigure.exclude
環境變量指定的配置,得到exclusions集合確保exclusions集合是configurations集合的子集,以及exclusions集合內的class均是存在於classpath環境的。否則異常會拋出
根據上述的exclusions集合篩選出未被過濾的configurations集合。
根據第7點篩選出來的configurations集合,再在autoConfigurationMetadata的基礎上針對
ConditionalOnClass
屬性篩選一輪
比如:org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration.ConditionalOnClass=org.springframework.cache.CacheManager
表示如果要應用CacheAutoConfiguration,則得保證org.springframework.cache.CacheManager類存在觸發AutoConfigurationImportEvent事件
返回篩選過后的configurations集合
筆者此處羅列下spring-boot-autoconfigure-2.0.3.REALEASE包中的spring.factories
中的EnableAutoConfiguration默認屬性,屬性太多,節選如下
# Auto Configure
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,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
...
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
...
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
...
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration
當然用戶也可以自定義去實現需要自動注入的配置類。
總結
@SpringBootApplication注解內含@EnableAutoConfiguration注解和@ComponentScan注解,所以這兩個注解結合再搭配上@Configuration注解便可以實現springboot的相關功能了。
在這之中,@EnableAutoConfiguration注解最為重要,其擴展性很好,方便springboot無縫對接不同的插件(NoSql插件、Web插件、JMX協議插件等等),讀者需要好好分析。
下節預告
通過查閱上述的多個自動注解,發現用到了@Conditional和AutoConfigureAfter注解,都屬於條件判斷,在深入理解springboot整合其他插件前,必須對此兩個注解有深刻的理解。