發布時間: 2018-12-19 17:54:27
?@SpringBootApplication SpringBoot的主配置類,SpringBoot就應該運行這個類的main方法來啟動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 @interface SpringBootApplication {}
@SpringBootConfiguration:表示這是一個Spring Boot的配置類;
Configuration:配置類注解;
Component:容器中的組件
@EnableAutoConfiguration:開啟自動配置功能,開啟了才會生效,是@AutoConfigurationPackage和@Import(AutoConfigurationImportSelector.class) 的組合;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
@AutoConfigurationPackage:自動配置包
@Import(AutoConfigurationPackages.Registrar.class):Spring的底層注解@Import,給容器中導入一個組件;導入的組件由AutoConfigurationPackages.Registrar.class完成;
new PackageImport(metadata).getPackageName()
計算的結果是@SpringBootApplication注解的類的包名
將主配置類(@SpringBootApplication標注的類)的所在包及下面所有子包里面的所有組件掃描到Spring容器;
@Import(AutoConfigurationImportSelector.class):給容器中導入組件,將所有需要導入的組件以全類名的方式返回;這些組件就會被添加到容器中;
?
SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
包含2個值:EnableAutoConfiguration.class,ClassLoader;
???