发布时间: 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;