SpringBoot-注解@EnableAutoConfiguration自动装配原理
参考文献
@EnableAutoConfiguration
注解
1 |
|
SpringBoot自动装配原理
-
基于
SpringBoot2.7x
-
@EnableAutoConfiguration
注解是 Spring Boot 自动装配机制的入口,它会启用 Spring Boot 的自动装配功能.@EnableAutoConfiguration
注解的主要作用是导入自动配置类,而这些自动配置类都包含在spring-boot-autoconfigure.jar
包中. -
@EnableAutoConfiguration
注解通过@Import
注解导入了AutoConfigurationImportSelector
类,这个类会通过SpringFactoriesLoader.loadFactoryNames
和ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add)
扫描classpath
下的META-INF/spring.factories
文件和META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件,读取其中的配置信息,然后根据这些配置信息,将这些类加载到IoC
容器中.值得注意的是并不是两个文件中所有的类一定都会被加载,还要结合spring-autoconfigure-metadata.properties
来判断某个 Bean 是否需要装配.如果符合条件,则将这个 Bean 自动装配到 IOC 容器中.- 具体来说,当 Spring Boot 应用启动时,它会自动扫描
classpath
下的META-INF/spring.factories
文件,读取其中的配置信息,并根据配置信息自动装配相应的 Bean。其中,spring.factories
文件中的每个配置项都包含一个 key-value 对,其中 key 是自动配置类的注解全名,value 是自动配置类的全限定类名。 - 而
org.springframework.boot.autoconfigure.AutoConfiguration.imports
的作用就是在这个过程中,指定需要额外导入的配置类。这些额外导入的配置类可以是自动配置类,也可以是普通的配置类。 - 通过
spring-autoconfigure-metadata.properties
来过滤
- 具体来说,当 Spring Boot 应用启动时,它会自动扫描
结合SpringBoot
启动流程分析@EnableAutoConfiguration
流程
入口org.springframework.boot.SpringApplication#run
1 | /** |
Spring
上下文核心方法org.springframework.context.support.AbstractApplicationContext#refresh
- 主要看
TODO 5
1 |
|
1 | /** |
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors
方法较长此处不展示,该方法会调用org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanDefinitionRegistryPostProcessors
来遍历执行postProcessor
的postProcessBeanDefinitionRegistry
方法.其中负责@Import
注解的postProcessor
为org.springframework.context.annotation.ConfigurationClassPostProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor
1 | /** |
1 | /** |
org.springframework.context.annotation.ConfigurationClassParser
1 | public void parse(Set<BeanDefinitionHolder> configCandidates) { |
- 进入解析方法最终会进入
org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass
方法- 该方法处理各种注解,如
@Component,@PropertySource,@ComponentScan,@Import
等
- 该方法处理各种注解,如
1 |
|
@Import
核心处理方法
- 注意点: 因为
AutoConfigurationImportSelector
实现了DeferredImportSelector
接口,因此不会直接执行selectImports
方法.DeferredImportSelector
接口用于支持延迟导入配置类的功能.- 在 Spring Framework 中,
@Import
注解用于导入其他配置类或者导入其他 Spring 配置。使用@Import
注解可以将其他类引入到当前的配置类中,这样就可以在当前配置类中使用被引入的类中的 Bean 了。但是,如果被引入的配置类中存在某些依赖还没有初始化完成,就会导致引入的 Bean 无法正常使用。 - 为了解决这个问题,Spring Framework 提供了
DeferredImportSelector
接口,用于支持延迟导入配置类的功能。DeferredImportSelector
接口继承自ImportSelector
接口,可以返回一个DeferredImportSelector.Group
对象,用于延迟加载配置类并解析其中的 BeanDefinition。 - 具体来说,
DeferredImportSelector.Group
接口有两个方法:getImports()
方法:用于返回需要导入的配置类的全限定类名。process()
方法:用于在应用程序上下文启动后处理被导入的配置类。
- 通过实现
DeferredImportSelector
接口和DeferredImportSelector.Group
接口,可以实现在应用程序上下文启动前或启动后,动态加载配置类中的 BeanDefinition,从而可以解决 Bean 初始化的依赖问题。
1 | private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass, |
- 解析完成后执行
this.deferredImportSelectorHandler.process();
1 | public void process() { |
1 | public void register(DeferredImportSelectorHolder deferredImport) { |
1 | public void processGroupImports() { |
1 | public Iterable<Group.Entry> getImports() { |
org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.AutoConfigurationGroup
1 |
|
AutoConfigurationImportSelector
类
- 通过
SpringFactoriesLoader#loadFactoryName(Class,ClassLoader)
方法读取所有META-INF/spring.factories
资源中@EnableAutoConfiguration
所关联的自动装配Class
集合 - 读取当前配置类所标注的
@EnableAutoConfiguration
属性exclude
和excludeName
,并与spring.autoconfigure.exclude
配置属性合并为自动装配Class排除集合 - 检查自动装配排除结核是否合法
- 排除候选自动装配Class集合中的排除名单
- 再次过滤候选自动装配Class集合中Class不存在的成员
1 | public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, |
org.springframework.core.io.support.SpringFactoriesLoader
类
- 加载
META-INF/spring.factories
文件
1 | public final class SpringFactoriesLoader { |
org.springframework.boot.context.annotation.ImportCandidates
- 加载
org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件
1 | /** |
@EnableAutoConfiguration
排序自动装配组件
- 绝对自动装配顺序--
@AutoConfigureOrder
- 相对自动装配顺序--
@AutoConfigureBefore
和@AutoConfigureAfter
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 HoleLin's Blog!