springboot自动装配原理剖析

  • @SpringBootConfiguration
  • @ComponentScan
  • @EnableAutoConfiguration
  • 总结:


springboot的核心注解:@SpringBootApplication

在@SpringBootApplication注解中有许多注解
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan 是核心的三个注解

本文重点讲述@EnableAutoConfiguration 注解

Spring boot 类扫描顺序 springboot扫描原理_spring

@SpringBootConfiguration

里面有一个**@Configuration**,所以也就是对spring原生注解的封装

Spring boot 类扫描顺序 springboot扫描原理_自动装配_02

@ComponentScan

默认扫描的是与该类同级的类或者同级包下的所有类,是spring的原生注解之一
Spring框架解析之@ComponentScan 用来定义IoC容器需要扫描哪些类文件。在这些类文件中,所有以@Component标注的对象和以@Bean标注的方法都会被自动加载到IoC容器中。

@EnableAutoConfiguration

一旦加上此注解,那么将会开启自动装配功能,简单点讲,Spring会试图在自己的classpath(类路径)下找到所有配置的Bean然后进行装配。

装配Bean时,会根据若干个(Conditional)定制规则来进行初始化。

Spring boot 类扫描顺序 springboot扫描原理_spring boot_03


在@EnableAutoConfiguration中通过这个@Import注解导入AutoConfigurationlmportSelector自动装配类点进AutoConfigurationlmportSelector类

他实现了DeferredImportSelector接口

Spring boot 类扫描顺序 springboot扫描原理_spring boot_04


DeferredImportSelector接口又继承了ImportSelector接口

Spring boot 类扫描顺序 springboot扫描原理_spring_05


在ImportSelector接口中有一个selectImports方法

Spring boot 类扫描顺序 springboot扫描原理_spring boot_06


在AutoConfigurationlmportSelector类中有核心自动配备核心方法selectImports

这个方法返回的是一个数组,里面装的是自动装配的包的路径,作用是找到满足配置的所有带注解的类,然后交给Spring进行处理

在selectImports这个方法中有一个getAutoConfigurationEntry方法

Spring boot 类扫描顺序 springboot扫描原理_spring boot_07


在getAutoConfigurationEntry方法中先判断是否进行自动装配,,这个方法中从META-INF/spring.factories文件中获取EnableAutoConfiguration所对应的configurations,然后完成自动装配

在getAutoConfigurationEntry方法中有一个getCandidateConfigurations方法

Spring boot 类扫描顺序 springboot扫描原理_spring boot_08


getCandidateConfigurations方法中有springboot内部工具类SpringFactoriesLoader类的loadFactoryNames方法

Spring Boot提供的一些JAR包,里面会带有文件META-INF/spring.factories。Spring Boot应用启动的时候,根据启动阶段不同的需求,框架就会调用SpringFactoriesLoader加载相应的工厂配置信息。

在Spring Boot应用使用了注解@EnableAutoConfiguration时,触发对SpringFactoriesLoader.loadFactoryNames()的调用来。

会读取META-INF/spring.factories下的EnableAutoConfiguration的配置,紧接着在进行排除与过滤,进而得到需要装配的类。最后让所有配置在META-INF/spring.factories下的AutoConfigurationImportListener执行AutoConfigurationImportEvent事件

(水平有限,这部分还是似懂非懂)

Spring boot 类扫描顺序 springboot扫描原理_Spring boot 类扫描顺序_09


Spring boot 类扫描顺序 springboot扫描原理_自动装配_10

总结:

  1. 在springboot的主配置类中有一个@SpringBootApplication注解
  2. 这SpringBootApplication注解中有三个重要注解: @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan
    @SpringBootConfiguration封装了spring原生的@Configuration注解 ,
    @ComponentScan注解用来定义IoC容器需要扫描哪些类文件。
  3. @EnableAutoConfiguration注解是自动装配的核心
    在@EnableAutoConfiguration中通过@Import注解导入AutoConfigurationlmportSelector自动装配类
    这个自动装配类实现了DeferredImportSelector接口,DeferredImportSelector接口又继承了ImportSelector接口
    在ImportSelector接口中有一个selectImports方法
  4. AutoConfigurationImportSelector类中重写了selectImports方法
    这个方法返回的是一个数组,里面装的是自动装配的包的路径,作用是找到满足配置的所有带注解的类,然后交给Spring进行处理
  5. 在selectImport方法中调用了getAutoConfigurationEntry方法,这个方法先判断是否进行自动装配,如果需要,会调用getCandidateConfigurations方法
  6. 在getCandidateConfigurations方法中调用SpringFactoriesLoader类
    SpringFactoriesLoader类会读取springbootJAR包里的META-INF/spring.factories文件下的EnableAutoConfiguration的配置确定要装配的类
  7. SpringBoot的自动化配置重度依赖@EnableAutoConfiguration注解、SpringFactoriesLoader类、META-INF/spring.factories文件等要素。