主入口類/啟動類
@SpringBootApplication
啟動類是一個組合注解
配置類也是容器中的一個組件
第一個注解@springbootconfigration
.
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration springboot定義的配置注解 @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication {
->configration
.
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration spring定義的配置類注解 public @interface SpringBootConfiguration {
第二個注解@enableautoconfiguration開啟自動配置功能
告訴s.b.開啟自動配置功能
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage 自動配置包,將SpringBootApplication主配置類所在包以及子包的所有子類掃描到spring容器
@Import({AutoConfigurationImportSelector.class}) 導入組件開啟自動配置類導包的選擇器
public @interface EnableAutoConfiguration {
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import({Registrar.class}) spring的底層注解,給容器中導入一個組件 public @interface AutoConfigurationPackage { }
假如在主配置類所在包package com.atguigu;外面package com;定義一個路徑/hello就掃描不進來了
@Import({AutoConfigurationImportSelector.class})將所有需要導入的組件以全類名的方式返回;這些組件被添加到容器中;最終給容器導入很多自動配置類(XXAutoConfiguration);給容器中導入這個場景需要的所有組件,並配置好這些組件;
有了自動配置類免去了手動編寫配置和注入功能組件的工作
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
configurations = this.removeDuplicates(configurations);
Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
this.checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = this.filter(configurations, autoConfigurationMetadata);
this.fireAutoConfigurationImportEvents(configurations, exclusions);
return StringUtils.toStringArray(configurations);
打斷點可以看到如下內容
進入getCandidateConfigurations方法
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
第一個參數:
protected Class<?> getSpringFactoriesLoaderFactoryClass() {
return EnableAutoConfiguration.class;
}
SpringFactoriesLoader.loadFactoryNames()
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, this.beanClassLoader)
從類加載器中獲取一個資源,把資源當成properties文件,然后拿出工廠的名字。
springboot在啟動的時候從META-INF/spring.factories獲取EnableAutoConfiguration指定的值
將這些值作為自動配置類 導入到容器中,自動配置類就生效,幫我們進行自動配置工作
進入擴展
想找到什么配置,都可以在擴展中meta-inf,spring.factories中這樣找
比如選擇其中的webmvcautoconfiguration,看到給容器中添加一個filter組件
比如添加解析器
一攬子解決方案全都自動配置
整體的解決方案自動配置都在這里
D:\Maven-Repository\org\springframework\boot\spring-boot-autoconfigure\2.0.2.RELEASE\spring-boot-autoconfigure-2.0.2.RELEASE.jar!\org\springframework\boot\autoconfigure
每一種怎么配的都在這里學習,也可以在其中改善