springboot学习二:@SpringBootApplication注解探秘

本章目的

探索springboot背后的注解,逐渐解开其神秘面纱。

系统版本

  1. jdk1.8
  2. springboot 2.1.6.RELEASE
  3. 开发工具(IntelliJ IDEA 2018.1.5 x64)
  4. apache maven(3.6.0,本章采用maven形式管理jar包,具体配置环境变量以及使用请自行查找资料)

开始。。。

我们首先分析入口类Application的启动注解@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 {
...
}

显然 @SpringBootApplication是一个复合注解,包括以下三个注解:

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan

下面我们一起来看下

@SpringBootConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

}

@SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类,
并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。

@ComponentScan

@ComponentScan自动扫描并加载符合条件的组件(比如@Component和@Repository等)或者bean定义,最终将这些bean定义加载到IoC容器中。是以前的<context:component-scan>(以前使用在XML中使用的标签,用来扫描包配置的平行支持)。

我们可以通过basePackages等属性来细粒度的定制@ComponentScan自动扫描的范围,如果不指定,则默认Spring框架实现会从声明@ComponentScan所在类的package进行扫描。

这两个注解,并不是 springboot特有的,spring时代已经有了,对我们来说并不陌生,今天咱们的主角是:@EnableAutoConfiguration

@EnableAutoConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

    //是否开启自动装配 默认为true,可在yml文件中设置
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    //根据class来排除
    Class<?>[] exclude() default {};

        //根据class name来排除
    String[] excludeName() default {};
}

@EnableAutoConfiguration
自动配置是springboot的最大亮点,完美的展示了CoC约定由于配置。此注解,正是开启自动配置的核心。

从源码中,可以看到注解@Import,@Import(AutoConfigurationImportSelector.class)

@AutoConfigurationImportSelector

    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        ...
        AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(autoConfigurationMetadata,
                annotationMetadata);
        return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
    }

    protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata,
            AnnotationMetadata annotationMetadata) {
        ...
        //获取类路径下spring.factories下key为EnableAutoConfiguration全限定名对应值
        List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
        ...
    }

    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    //EnableAutoConfiguration.class
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
            getBeanClassLoader());
    ...
    }

@SpringFactoriesLoader

    public static <T> List<T> loadFactories(Class<T> factoryClass, @Nullable ClassLoader classLoader) {
        ...
        List<String> factoryNames = loadFactoryNames(factoryClass, classLoaderToUse);
        ...
    }
    public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
        String factoryClassName = factoryClass.getName();
        return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
    }

    private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
            ...
            //读取META-INF/spring.factories文件的配置,并返回一个字符串数组
            Enumeration<URL> urls = (classLoader != null ?
                    classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
                    ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
            result = new LinkedMultiValueMap<>();
            ...
    }

至此,springboot自动装配的神秘面纱已经解开。

总结:@EnableAutoConfiguration作用

  • 从classpath中搜索所有META-INF/spring.factories配置文件,将其中key为org.springframework.boot.autoconfigure.EnableAutoConfiguration对应的配置项加载到spring容器。
  • spring.boot.enableautoconfiguration为true(默认为true)的时候,才启用自动配置
  • 自定义排除某些不需要的自动配置
    • 根据class来排除(exclude)
    • 根据class name来排除(excludeName)

代码关键点

  1. ImportSelector(AutoConfigurationImportSelector) 该接口的方法的返回值都会被纳入到spring容器管理中
  2. SpringFactoriesLoader 从classpath中搜索所有META-INF/spring.factories配置文件,并读取配置。

疑问

  • @importImportSelectorImportBeanDefinitionRegistrar各有什么用?
  • spring的bpp是什么?
  • Enable*的原理是什么?

下一章,我们带着这些疑问,继续探索源码。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容