Open zyllt opened 5 years ago
SpringBootApplication注解是定义在spring-boot-autoconfigure模块中,通常被用作启动一个spring boot项目main方法所在类的注解。例如:
@SpringBootApplication public class SimpleMainTests { public static void main(String[] args) { SpringApplication.run(SimpleMainTests.class, new String[]{}); }}
Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.
指示一个Configuration类,该类声明一个或多个@Bean方法,并触发EnableAutoConfiguration自动配置和ComponentScan组件扫描。其实就是一个复合注解,把@Configuration、@EnableAutoConfiguration和@ComponentScan放在了一起。
看源码 @SpringBootConfiguration @EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
@SpringBootConfiguration是@Configuration的一种继承,在spring boot项目使用
@ComponentScan还是熟悉的味道,添加了2个custom的excludeFilter。其中TypeExcludeFilter主要是用在spring boot test中,不用关注。AutoConfigurationExcludeFilter是让自动配置的类不需要被ComponentScan扫描,它们有自己的注册方式。
@EnableAutoConfiguration 顾名思义就是用来启用spring boot的自动配置功能。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {
@EnableAutoConfiguration 其实就是引入了一个AutoConfigurationImportSelector类,从名称其实就可以看出它实现了ImportSelector,这接口的作用其实就是用来选择那些@Configuration类被引入。but AutoConfigurationImportSelector 其实是实现了DeferredImportSelector接口,DeferredImportSelector是ImportSelector的一个变体,它是在所有的@Configuration bean被处理完之后才运行的,主要是用来处理Conditional
基于spring boot 2.0.2