SpringBoot自动装配原理

@SpringBootApplication相当于

@SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan

●@EnableAutoConfiguration:启用 SpringBoot 的自动配置机制

●@ComponentScan: 扫描被@Component (@Service,@Controller)注解的 bean,注解默认会扫描该类所在的包下所有的类。

●@Configuration:允许在上下文中注册额外的 bean 或导入其他配置类

ComponentScan如果不设值的话默认扫描@ComponentScan注解所在类的同级类和同级目录下的所有类,所以对于一个Spring Boot项目,一般会把入口类放在顶层目录中,这样就能够保证源码目录下的所有类都能够被扫描到。

@EnableAutoConfiguration 注解通过 Spring 提供的 @Import 注解导入了AutoConfigurationImportSelector类(@Import 注解可以导入配置类或者 Bean 到当前类中)。

AutoConfigurationImportSelector类中getCandidateConfigurations方法会将所有自动配置类的信息以 List 的形式返回。这些配置信息会被 Spring 容器作 bean 来管理。

spring-boot-autoconfigure.jar 包中的 META-INF/spring.factories 里面默认配置了很多aoto-configuration,如下:

复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
......
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration

不光是这个依赖下的META-INF/spring.factories被读取到,所有 Spring Boot Starter 下的META-INF/spring.factories都会被读取到。

总的来说,@EnableAutoConfiguration完成了一下功能:

从classpath中搜寻所有的 META-INF/spring.factories配置文件,并将其中org.springframework.boot.autoconfigure.EnableutoConfiguration 对应的配置项通过反射实例化为对应的标注了@Configuration的JavaConfig形式的IoC容器配置类,然后汇总为一个并加载到IoC容器。

满足条件才会加载:

  • @SpringBootConfiguration 通过与 @Bean 结合完成Bean的 JavaConfig配置;
  • @ComponentScan 通过范围扫描的方式,扫描特定注解注释的类,将其注册到Spring容器;
  • @EnableAutoConfiguration 通过 spring.factories 的配置,并结合 @Condition 条件,完成bean的注册;
  • @Import 通过导入的方式,将指定的class注册解析到Spring容器;

SpringBoot自动配置原理,我的理解是这样的:

1、引入Starter组件

2、SpringBoot基于约定去Starter组件的路径下(META-INF/spring.factories)去找配置类

3、SpringBoot使用ImportSelector去导入这些配置类,并根据@Conditional动态加载配置类里面的Bean到容器

SpringBoot启动流程

相关推荐
摇滚侠1 天前
Spring Boot 3零基础教程,Spring Boot 日志的归档与切割,笔记22
spring boot·redis·笔记
lang201509281 天前
Spring Boot开发利器:devtools全解析(续)
spring boot
皮皮林5511 天前
SpringBoot启动优化7板斧:砍掉70%启动时间的魔鬼实践
spring boot
程序员小凯1 天前
Spring Boot消息队列与事件驱动详解
java·spring boot·后端
计算机学姐1 天前
基于微信小程序的垃圾分类管理系统【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
i学长的猫1 天前
Spring Boot 布隆过滤器最佳实践指南
spring boot·后端·哈希算法
Mr_hwt_1231 天前
spring boot框架中本地缓存@Cacheable原理与踩坑点详细解析
java·spring boot·后端·缓存
zl9798991 天前
SpringBoot-自动配置原理
java·spring boot·spring
李贺梖梖1 天前
Spring初始
spring
武昌库里写JAVA1 天前
C语言 #pragma once - C语言零基础入门教程
vue.js·spring boot·sql·layui·课程设计