Spring Boot 自动装配流程
自动装配目标
无需手动 XML 或大量 @Configuration 配置,Spring Boot 会根据项目依赖、运行环境和配置文件,自动创建并装配框架内置组件到 IOC 容器。
核心依赖链路:
@EnableAutoConfigurationAutoConfigurationImportSelector选择器- 自动配置清单加载
- 条件注解过滤
@ConfigurationProperties配置绑定- Bean 注册到 Spring IOC 容器
自动装配的整体架构可以理解为"启动入口触发开关,选择器加载候选配置,条件注解决定是否生效,最终把 Bean 放入容器"。
#mermaid-svg-1XbDyJHhGFS6P0P8{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-1XbDyJHhGFS6P0P8 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-1XbDyJHhGFS6P0P8 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-1XbDyJHhGFS6P0P8 .error-icon{fill:#552222;}#mermaid-svg-1XbDyJHhGFS6P0P8 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-1XbDyJHhGFS6P0P8 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-1XbDyJHhGFS6P0P8 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-1XbDyJHhGFS6P0P8 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-1XbDyJHhGFS6P0P8 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-1XbDyJHhGFS6P0P8 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-1XbDyJHhGFS6P0P8 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-1XbDyJHhGFS6P0P8 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-1XbDyJHhGFS6P0P8 .marker.cross{stroke:#333333;}#mermaid-svg-1XbDyJHhGFS6P0P8 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-1XbDyJHhGFS6P0P8 p{margin:0;}#mermaid-svg-1XbDyJHhGFS6P0P8 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-1XbDyJHhGFS6P0P8 .cluster-label text{fill:#333;}#mermaid-svg-1XbDyJHhGFS6P0P8 .cluster-label span{color:#333;}#mermaid-svg-1XbDyJHhGFS6P0P8 .cluster-label span p{background-color:transparent;}#mermaid-svg-1XbDyJHhGFS6P0P8 .label text,#mermaid-svg-1XbDyJHhGFS6P0P8 span{fill:#333;color:#333;}#mermaid-svg-1XbDyJHhGFS6P0P8 .node rect,#mermaid-svg-1XbDyJHhGFS6P0P8 .node circle,#mermaid-svg-1XbDyJHhGFS6P0P8 .node ellipse,#mermaid-svg-1XbDyJHhGFS6P0P8 .node polygon,#mermaid-svg-1XbDyJHhGFS6P0P8 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-1XbDyJHhGFS6P0P8 .rough-node .label text,#mermaid-svg-1XbDyJHhGFS6P0P8 .node .label text,#mermaid-svg-1XbDyJHhGFS6P0P8 .image-shape .label,#mermaid-svg-1XbDyJHhGFS6P0P8 .icon-shape .label{text-anchor:middle;}#mermaid-svg-1XbDyJHhGFS6P0P8 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-1XbDyJHhGFS6P0P8 .rough-node .label,#mermaid-svg-1XbDyJHhGFS6P0P8 .node .label,#mermaid-svg-1XbDyJHhGFS6P0P8 .image-shape .label,#mermaid-svg-1XbDyJHhGFS6P0P8 .icon-shape .label{text-align:center;}#mermaid-svg-1XbDyJHhGFS6P0P8 .node.clickable{cursor:pointer;}#mermaid-svg-1XbDyJHhGFS6P0P8 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-1XbDyJHhGFS6P0P8 .arrowheadPath{fill:#333333;}#mermaid-svg-1XbDyJHhGFS6P0P8 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-1XbDyJHhGFS6P0P8 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-1XbDyJHhGFS6P0P8 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-1XbDyJHhGFS6P0P8 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-1XbDyJHhGFS6P0P8 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-1XbDyJHhGFS6P0P8 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-1XbDyJHhGFS6P0P8 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-1XbDyJHhGFS6P0P8 .cluster text{fill:#333;}#mermaid-svg-1XbDyJHhGFS6P0P8 .cluster span{color:#333;}#mermaid-svg-1XbDyJHhGFS6P0P8 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-1XbDyJHhGFS6P0P8 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-1XbDyJHhGFS6P0P8 rect.text{fill:none;stroke-width:0;}#mermaid-svg-1XbDyJHhGFS6P0P8 .icon-shape,#mermaid-svg-1XbDyJHhGFS6P0P8 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-1XbDyJHhGFS6P0P8 .icon-shape p,#mermaid-svg-1XbDyJHhGFS6P0P8 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-1XbDyJHhGFS6P0P8 .icon-shape .label rect,#mermaid-svg-1XbDyJHhGFS6P0P8 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-1XbDyJHhGFS6P0P8 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-1XbDyJHhGFS6P0P8 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-1XbDyJHhGFS6P0P8 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} SpringApplication.run()
@SpringBootApplication
@EnableAutoConfiguration
AutoConfigurationImportSelector
读取自动配置清单
条件注解过滤
解析 AutoConfiguration
@Bean 方法创建组件
注册到 Spring IOC 容器
一、整体总流程概览
- 启动类标注
@SpringBootApplication,触发三大能力:包扫描、配置类标记、开启自动装配。 - 解析
@EnableAutoConfiguration,委托AutoConfigurationImportSelector执行自动配置类加载。 - 加载自动配置类候选清单,Spring Boot 2.7 前后对应两种文件方式。
- 对候选配置类进行过滤、去重、排除。
- 结合条件注解做环境判定,筛选出当前环境真正生效的自动配置类。
- 生效的自动配置类作为标准
@Configuration配置类,解析内部@Bean方法。 - 结合
@ConfigurationProperties绑定application.yml/application.properties外部配置。 - 将最终 Bean 注册到 Spring IOC 容器,完成自动装配。
#mermaid-svg-anWk3zDWckdosPyV{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-anWk3zDWckdosPyV .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-anWk3zDWckdosPyV .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-anWk3zDWckdosPyV .error-icon{fill:#552222;}#mermaid-svg-anWk3zDWckdosPyV .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-anWk3zDWckdosPyV .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-anWk3zDWckdosPyV .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-anWk3zDWckdosPyV .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-anWk3zDWckdosPyV .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-anWk3zDWckdosPyV .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-anWk3zDWckdosPyV .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-anWk3zDWckdosPyV .marker{fill:#333333;stroke:#333333;}#mermaid-svg-anWk3zDWckdosPyV .marker.cross{stroke:#333333;}#mermaid-svg-anWk3zDWckdosPyV svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-anWk3zDWckdosPyV p{margin:0;}#mermaid-svg-anWk3zDWckdosPyV .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-anWk3zDWckdosPyV .cluster-label text{fill:#333;}#mermaid-svg-anWk3zDWckdosPyV .cluster-label span{color:#333;}#mermaid-svg-anWk3zDWckdosPyV .cluster-label span p{background-color:transparent;}#mermaid-svg-anWk3zDWckdosPyV .label text,#mermaid-svg-anWk3zDWckdosPyV span{fill:#333;color:#333;}#mermaid-svg-anWk3zDWckdosPyV .node rect,#mermaid-svg-anWk3zDWckdosPyV .node circle,#mermaid-svg-anWk3zDWckdosPyV .node ellipse,#mermaid-svg-anWk3zDWckdosPyV .node polygon,#mermaid-svg-anWk3zDWckdosPyV .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-anWk3zDWckdosPyV .rough-node .label text,#mermaid-svg-anWk3zDWckdosPyV .node .label text,#mermaid-svg-anWk3zDWckdosPyV .image-shape .label,#mermaid-svg-anWk3zDWckdosPyV .icon-shape .label{text-anchor:middle;}#mermaid-svg-anWk3zDWckdosPyV .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-anWk3zDWckdosPyV .rough-node .label,#mermaid-svg-anWk3zDWckdosPyV .node .label,#mermaid-svg-anWk3zDWckdosPyV .image-shape .label,#mermaid-svg-anWk3zDWckdosPyV .icon-shape .label{text-align:center;}#mermaid-svg-anWk3zDWckdosPyV .node.clickable{cursor:pointer;}#mermaid-svg-anWk3zDWckdosPyV .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-anWk3zDWckdosPyV .arrowheadPath{fill:#333333;}#mermaid-svg-anWk3zDWckdosPyV .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-anWk3zDWckdosPyV .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-anWk3zDWckdosPyV .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-anWk3zDWckdosPyV .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-anWk3zDWckdosPyV .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-anWk3zDWckdosPyV .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-anWk3zDWckdosPyV .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-anWk3zDWckdosPyV .cluster text{fill:#333;}#mermaid-svg-anWk3zDWckdosPyV .cluster span{color:#333;}#mermaid-svg-anWk3zDWckdosPyV div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-anWk3zDWckdosPyV .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-anWk3zDWckdosPyV rect.text{fill:none;stroke-width:0;}#mermaid-svg-anWk3zDWckdosPyV .icon-shape,#mermaid-svg-anWk3zDWckdosPyV .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-anWk3zDWckdosPyV .icon-shape p,#mermaid-svg-anWk3zDWckdosPyV .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-anWk3zDWckdosPyV .icon-shape .label rect,#mermaid-svg-anWk3zDWckdosPyV .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-anWk3zDWckdosPyV .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-anWk3zDWckdosPyV .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-anWk3zDWckdosPyV :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 1. 启动应用
2. 解析 @SpringBootApplication
3. 激活 @EnableAutoConfiguration
4. 执行 AutoConfigurationImportSelector
5. 加载候选自动配置类
6. 排除 / 去重 / 校验
7. 条件注解按需筛选
8. 解析 @Configuration 与 @Bean
9. 绑定外部配置
10. Bean 注册到 IOC 容器
二、分步详细拆解(带源码逻辑)
步骤 1:启动入口 ------ @SpringBootApplication 组合注解
项目启动类上的核心注解:
java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
点开 @SpringBootApplication 源码,它是三元组合注解:
java
@SpringBootConfiguration // 等价 @Configuration,标记当前类为配置类
@EnableAutoConfiguration // 自动装配核心开关
@ComponentScan // 自动扫描当前包及子包下所有标注 @Component 等组件
public @interface SpringBootApplication {}
三个注解分工:
| 注解 | 作用 |
|---|---|
@ComponentScan |
扫描业务代码中的自定义 Bean。 |
@SpringBootConfiguration |
标识启动类为配置类。 |
@EnableAutoConfiguration |
开启自动装配功能,是整个机制的起点。 |
步骤 2:核心开关 ------ @EnableAutoConfiguration
进入该注解源码:
java
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
关键点:
- Spring 中
@Import的作用是向容器中导入第三方配置类或选择器类。 - 这里直接导入
AutoConfigurationImportSelector,即自动配置导入选择器。 - 所有自动装配逻辑,全部由这个选择器驱动执行。
补充:@EnableXXX 系列注解是 Spring 的通用范式,通常通过 @Import 导入选择器或配置类,从而开启某一功能。
#mermaid-svg-w7g4OWvSqGEMEqfO{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-w7g4OWvSqGEMEqfO .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-w7g4OWvSqGEMEqfO .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-w7g4OWvSqGEMEqfO .error-icon{fill:#552222;}#mermaid-svg-w7g4OWvSqGEMEqfO .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-w7g4OWvSqGEMEqfO .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-w7g4OWvSqGEMEqfO .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-w7g4OWvSqGEMEqfO .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-w7g4OWvSqGEMEqfO .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-w7g4OWvSqGEMEqfO .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-w7g4OWvSqGEMEqfO .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-w7g4OWvSqGEMEqfO .marker{fill:#333333;stroke:#333333;}#mermaid-svg-w7g4OWvSqGEMEqfO .marker.cross{stroke:#333333;}#mermaid-svg-w7g4OWvSqGEMEqfO svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-w7g4OWvSqGEMEqfO p{margin:0;}#mermaid-svg-w7g4OWvSqGEMEqfO .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-w7g4OWvSqGEMEqfO .cluster-label text{fill:#333;}#mermaid-svg-w7g4OWvSqGEMEqfO .cluster-label span{color:#333;}#mermaid-svg-w7g4OWvSqGEMEqfO .cluster-label span p{background-color:transparent;}#mermaid-svg-w7g4OWvSqGEMEqfO .label text,#mermaid-svg-w7g4OWvSqGEMEqfO span{fill:#333;color:#333;}#mermaid-svg-w7g4OWvSqGEMEqfO .node rect,#mermaid-svg-w7g4OWvSqGEMEqfO .node circle,#mermaid-svg-w7g4OWvSqGEMEqfO .node ellipse,#mermaid-svg-w7g4OWvSqGEMEqfO .node polygon,#mermaid-svg-w7g4OWvSqGEMEqfO .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-w7g4OWvSqGEMEqfO .rough-node .label text,#mermaid-svg-w7g4OWvSqGEMEqfO .node .label text,#mermaid-svg-w7g4OWvSqGEMEqfO .image-shape .label,#mermaid-svg-w7g4OWvSqGEMEqfO .icon-shape .label{text-anchor:middle;}#mermaid-svg-w7g4OWvSqGEMEqfO .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-w7g4OWvSqGEMEqfO .rough-node .label,#mermaid-svg-w7g4OWvSqGEMEqfO .node .label,#mermaid-svg-w7g4OWvSqGEMEqfO .image-shape .label,#mermaid-svg-w7g4OWvSqGEMEqfO .icon-shape .label{text-align:center;}#mermaid-svg-w7g4OWvSqGEMEqfO .node.clickable{cursor:pointer;}#mermaid-svg-w7g4OWvSqGEMEqfO .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-w7g4OWvSqGEMEqfO .arrowheadPath{fill:#333333;}#mermaid-svg-w7g4OWvSqGEMEqfO .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-w7g4OWvSqGEMEqfO .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-w7g4OWvSqGEMEqfO .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-w7g4OWvSqGEMEqfO .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-w7g4OWvSqGEMEqfO .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-w7g4OWvSqGEMEqfO .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-w7g4OWvSqGEMEqfO .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-w7g4OWvSqGEMEqfO .cluster text{fill:#333;}#mermaid-svg-w7g4OWvSqGEMEqfO .cluster span{color:#333;}#mermaid-svg-w7g4OWvSqGEMEqfO div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-w7g4OWvSqGEMEqfO .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-w7g4OWvSqGEMEqfO rect.text{fill:none;stroke-width:0;}#mermaid-svg-w7g4OWvSqGEMEqfO .icon-shape,#mermaid-svg-w7g4OWvSqGEMEqfO .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-w7g4OWvSqGEMEqfO .icon-shape p,#mermaid-svg-w7g4OWvSqGEMEqfO .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-w7g4OWvSqGEMEqfO .icon-shape .label rect,#mermaid-svg-w7g4OWvSqGEMEqfO .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-w7g4OWvSqGEMEqfO .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-w7g4OWvSqGEMEqfO .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-w7g4OWvSqGEMEqfO :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} @SpringBootApplication
@SpringBootConfiguration
@ComponentScan
@EnableAutoConfiguration
@Import(AutoConfigurationImportSelector.class)
加载并筛选自动配置类
启动类作为配置类
扫描业务自定义 Bean
导入框架默认配置 Bean
IOC 容器
步骤 3:执行选择器 ------ AutoConfigurationImportSelector
AutoConfigurationImportSelector 实现了 Spring 的 DeferredImportSelector,也就是延迟导入选择器。
延迟导入的含义是:先执行 @ComponentScan 包扫描和普通配置类解析,最后再执行自动配置类导入。这样可以保证:
用户自定义 Bean 优先级 > 框架自动装配 Bean
它的核心方法是 selectImports(),执行逻辑分为 4 小段:
- 获取所有候选自动配置类全类名列表。
- 处理
exclude排除项,手动排除指定自动配置类。 - 去重、过滤无效类。
- 返回最终待加载的自动配置类名称数组,交给 Spring 解析。
3.1 加载候选自动配置类清单(版本区分重点)
Spring Boot 分两个版本方案读取配置清单,这是面试高频考点。
| 版本 | 配置方式 | 配置文件路径 | 特点 |
|---|---|---|---|
| Spring Boot 2.7 之前 | spring.factories |
META-INF/spring.factories |
经典方案,基于 SpringFactoriesLoader 加载。 |
| Spring Boot 2.7 及以上 | AutoConfiguration.imports |
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports |
专用导入文件,每行一个自动配置类,结构更简单,加载效率更高。 |
#mermaid-svg-axmwHiEXcw8iYYIj{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-axmwHiEXcw8iYYIj .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-axmwHiEXcw8iYYIj .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-axmwHiEXcw8iYYIj .error-icon{fill:#552222;}#mermaid-svg-axmwHiEXcw8iYYIj .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-axmwHiEXcw8iYYIj .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-axmwHiEXcw8iYYIj .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-axmwHiEXcw8iYYIj .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-axmwHiEXcw8iYYIj .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-axmwHiEXcw8iYYIj .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-axmwHiEXcw8iYYIj .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-axmwHiEXcw8iYYIj .marker{fill:#333333;stroke:#333333;}#mermaid-svg-axmwHiEXcw8iYYIj .marker.cross{stroke:#333333;}#mermaid-svg-axmwHiEXcw8iYYIj svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-axmwHiEXcw8iYYIj p{margin:0;}#mermaid-svg-axmwHiEXcw8iYYIj .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-axmwHiEXcw8iYYIj .cluster-label text{fill:#333;}#mermaid-svg-axmwHiEXcw8iYYIj .cluster-label span{color:#333;}#mermaid-svg-axmwHiEXcw8iYYIj .cluster-label span p{background-color:transparent;}#mermaid-svg-axmwHiEXcw8iYYIj .label text,#mermaid-svg-axmwHiEXcw8iYYIj span{fill:#333;color:#333;}#mermaid-svg-axmwHiEXcw8iYYIj .node rect,#mermaid-svg-axmwHiEXcw8iYYIj .node circle,#mermaid-svg-axmwHiEXcw8iYYIj .node ellipse,#mermaid-svg-axmwHiEXcw8iYYIj .node polygon,#mermaid-svg-axmwHiEXcw8iYYIj .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-axmwHiEXcw8iYYIj .rough-node .label text,#mermaid-svg-axmwHiEXcw8iYYIj .node .label text,#mermaid-svg-axmwHiEXcw8iYYIj .image-shape .label,#mermaid-svg-axmwHiEXcw8iYYIj .icon-shape .label{text-anchor:middle;}#mermaid-svg-axmwHiEXcw8iYYIj .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-axmwHiEXcw8iYYIj .rough-node .label,#mermaid-svg-axmwHiEXcw8iYYIj .node .label,#mermaid-svg-axmwHiEXcw8iYYIj .image-shape .label,#mermaid-svg-axmwHiEXcw8iYYIj .icon-shape .label{text-align:center;}#mermaid-svg-axmwHiEXcw8iYYIj .node.clickable{cursor:pointer;}#mermaid-svg-axmwHiEXcw8iYYIj .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-axmwHiEXcw8iYYIj .arrowheadPath{fill:#333333;}#mermaid-svg-axmwHiEXcw8iYYIj .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-axmwHiEXcw8iYYIj .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-axmwHiEXcw8iYYIj .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-axmwHiEXcw8iYYIj .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-axmwHiEXcw8iYYIj .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-axmwHiEXcw8iYYIj .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-axmwHiEXcw8iYYIj .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-axmwHiEXcw8iYYIj .cluster text{fill:#333;}#mermaid-svg-axmwHiEXcw8iYYIj .cluster span{color:#333;}#mermaid-svg-axmwHiEXcw8iYYIj div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-axmwHiEXcw8iYYIj .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-axmwHiEXcw8iYYIj rect.text{fill:none;stroke-width:0;}#mermaid-svg-axmwHiEXcw8iYYIj .icon-shape,#mermaid-svg-axmwHiEXcw8iYYIj .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-axmwHiEXcw8iYYIj .icon-shape p,#mermaid-svg-axmwHiEXcw8iYYIj .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-axmwHiEXcw8iYYIj .icon-shape .label rect,#mermaid-svg-axmwHiEXcw8iYYIj .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-axmwHiEXcw8iYYIj .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-axmwHiEXcw8iYYIj .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-axmwHiEXcw8iYYIj :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 2.7 之前
2.7 及以上
AutoConfigurationImportSelector
Spring Boot 版本
读取 META-INF/spring.factories
读取 AutoConfiguration.imports
候选自动配置类列表
排除 exclude
去重与类存在校验
进入条件注解过滤
方案 A:Spring Boot 2.7 之前(经典方案:spring.factories)
底层使用 SpringFactoriesLoader 加载,它是 Spring 自定义 SPI 实现。
- 配置文件路径:
META-INF/spring.factories - 文件位置:
spring-boot-autoconfigure核心自动装配包内 - 固定 Key:
org.springframework.boot.autoconfigure.EnableAutoConfiguration - Value:一长串所有内置自动配置类全限定类名,例如
WebMvcAutoConfiguration、DataSourceAutoConfiguration、RedisAutoConfiguration
工作逻辑:SpringFactoriesLoader 扫描 classpath 下所有 jar 包中的 spring.factories,根据固定 Key 取出对应的类名集合,作为全部候选自动配置类。
方案 B:Spring Boot 2.7 及以上(新方案:AutoConfiguration.imports)
官方废弃 spring.factories 对自动配置的承载,改用专用导入文件:
text
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
该文件采用纯文本格式,每行一个自动配置类全类名,结构更简单,加载效率更高。
注意:只是配置文件变了,后续筛选、条件判断流程完全不变。
步骤 4:过滤与排除配置类
拿到全量候选列表后,框架做两层过滤。
4.1 手动排除
支持两种方式排除不需要的自动配置类:
java
// 方式 1:注解 exclude
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
properties
# 方式 2:配置文件排除
spring.autoconfigure.exclude=xxx.xxx.XXXAutoConfiguration
选择器会从列表中移除被排除的类。
4.2 基础去重与校验
移除重复类、不存在的类,得到初步筛选后的配置类列表。
步骤 5:核心精髓 ------ 条件注解按需生效
此时列表里仍然有很多配置类,但它们不会全部生效。Spring Boot 会通过 Spring 原生条件注解(@Conditional 系列),结合当前项目环境、依赖和配置做动态判断,只保留符合条件的配置类。
常用核心条件注解如下:
| 条件注解 | 作用 | 典型场景 |
|---|---|---|
@ConditionalOnClass(XXX.class) |
指定类存在时生效。 | 判断是否引入对应 Starter 依赖。 |
@ConditionalOnMissingBean(XXX.class) |
容器中没有指定 Bean 时才创建。 | 允许开发者覆盖默认自动配置。 |
@ConditionalOnWebApplication |
仅在 Web 项目环境下生效。 | 区分普通 Java 项目和 Web 项目。 |
@ConditionalOnProperty |
根据配置属性值决定是否开启配置。 | 通过 application.yml 控制功能开关。 |
示例说明:
java
@ConditionalOnClass(XXX.class)
类存在则生效。其原理是判断 classpath 中是否存在指定 Class,等价于判断是否引入对应 Starter 依赖。
例如:DataSourceAutoConfiguration 标注 @ConditionalOnClass(DataSource.class)。如果没有引入数据库驱动或数据源依赖,则类不存在,该自动配置会直接跳过。
java
@ConditionalOnMissingBean(XXX.class)
容器中没有该 Bean 才创建。其优先级规则是:
用户手动定义的 Bean > 框架自动装配 Bean
执行逻辑:
- Spring 在解析每个
XXXAutoConfiguration配置类前,先校验其上的所有条件注解。 - 全部条件满足时,该配置类保留,进入下一步解析。
- 任意条件不满足时,直接跳过该配置类,不加载。
经过这一步,列表会大幅缩减,只剩下当前项目真正需要的自动配置类。
#mermaid-svg-MT3H6TobzAG3kRvh{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-MT3H6TobzAG3kRvh .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-MT3H6TobzAG3kRvh .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-MT3H6TobzAG3kRvh .error-icon{fill:#552222;}#mermaid-svg-MT3H6TobzAG3kRvh .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MT3H6TobzAG3kRvh .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-MT3H6TobzAG3kRvh .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MT3H6TobzAG3kRvh .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MT3H6TobzAG3kRvh .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-MT3H6TobzAG3kRvh .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MT3H6TobzAG3kRvh .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MT3H6TobzAG3kRvh .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MT3H6TobzAG3kRvh .marker.cross{stroke:#333333;}#mermaid-svg-MT3H6TobzAG3kRvh svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MT3H6TobzAG3kRvh p{margin:0;}#mermaid-svg-MT3H6TobzAG3kRvh .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-MT3H6TobzAG3kRvh .cluster-label text{fill:#333;}#mermaid-svg-MT3H6TobzAG3kRvh .cluster-label span{color:#333;}#mermaid-svg-MT3H6TobzAG3kRvh .cluster-label span p{background-color:transparent;}#mermaid-svg-MT3H6TobzAG3kRvh .label text,#mermaid-svg-MT3H6TobzAG3kRvh span{fill:#333;color:#333;}#mermaid-svg-MT3H6TobzAG3kRvh .node rect,#mermaid-svg-MT3H6TobzAG3kRvh .node circle,#mermaid-svg-MT3H6TobzAG3kRvh .node ellipse,#mermaid-svg-MT3H6TobzAG3kRvh .node polygon,#mermaid-svg-MT3H6TobzAG3kRvh .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-MT3H6TobzAG3kRvh .rough-node .label text,#mermaid-svg-MT3H6TobzAG3kRvh .node .label text,#mermaid-svg-MT3H6TobzAG3kRvh .image-shape .label,#mermaid-svg-MT3H6TobzAG3kRvh .icon-shape .label{text-anchor:middle;}#mermaid-svg-MT3H6TobzAG3kRvh .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-MT3H6TobzAG3kRvh .rough-node .label,#mermaid-svg-MT3H6TobzAG3kRvh .node .label,#mermaid-svg-MT3H6TobzAG3kRvh .image-shape .label,#mermaid-svg-MT3H6TobzAG3kRvh .icon-shape .label{text-align:center;}#mermaid-svg-MT3H6TobzAG3kRvh .node.clickable{cursor:pointer;}#mermaid-svg-MT3H6TobzAG3kRvh .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-MT3H6TobzAG3kRvh .arrowheadPath{fill:#333333;}#mermaid-svg-MT3H6TobzAG3kRvh .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-MT3H6TobzAG3kRvh .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-MT3H6TobzAG3kRvh .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MT3H6TobzAG3kRvh .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-MT3H6TobzAG3kRvh .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MT3H6TobzAG3kRvh .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-MT3H6TobzAG3kRvh .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-MT3H6TobzAG3kRvh .cluster text{fill:#333;}#mermaid-svg-MT3H6TobzAG3kRvh .cluster span{color:#333;}#mermaid-svg-MT3H6TobzAG3kRvh div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-MT3H6TobzAG3kRvh .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-MT3H6TobzAG3kRvh rect.text{fill:none;stroke-width:0;}#mermaid-svg-MT3H6TobzAG3kRvh .icon-shape,#mermaid-svg-MT3H6TobzAG3kRvh .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MT3H6TobzAG3kRvh .icon-shape p,#mermaid-svg-MT3H6TobzAG3kRvh .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-MT3H6TobzAG3kRvh .icon-shape .label rect,#mermaid-svg-MT3H6TobzAG3kRvh .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MT3H6TobzAG3kRvh .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-MT3H6TobzAG3kRvh .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-MT3H6TobzAG3kRvh :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 否
是
否
是
已定义
未定义
候选配置类
XXXAutoConfiguration
@ConditionalOnClass
依赖类是否存在?
跳过该自动配置类
@ConditionalOnProperty
配置是否开启?
@ConditionalOnMissingBean
用户是否已定义 Bean?
配置类生效
解析 @Bean 方法
注册默认组件
步骤 6:解析自动配置类(@Configuration + @Bean 注册组件)
通过条件校验的类,本质都是标准 Spring 配置类。所有 XXXAutoConfiguration 都会标注 @Configuration。
java
@Configuration
@ConditionalOnClass(DataSource.class) // 有数据源类才生效
@ConditionalOnMissingBean(DataSource.class) // 用户没自定义数据源才生效
public class DataSourceAutoConfiguration {
// 自动创建数据源 Bean
@Bean
public DataSource dataSource(DataSourceProperties properties) {
// 根据配置参数构建数据源
return new HikariDataSource(...);
}
}
执行流程:
- Spring 将
XXXAutoConfiguration当作普通@Configuration解析。 - 扫描内部所有
@Bean方法。 - 执行方法,创建对象。
- 将对象以 Bean 形式存入 Spring IOC 容器。
步骤 7:外部配置绑定(application.yml / application.properties)
自动配置的参数全部来自项目配置文件,依靠 @ConfigurationProperties 实现绑定:
- 框架提供属性绑定实体类,例如
DataSourceProperties。 - 该类添加
@ConfigurationProperties(prefix = "spring.datasource")。 - 自动将
application.yml中spring.datasource开头的配置注入到实体类字段。 - 自动配置类中的
@Bean方法直接注入该属性类,读取配置参数创建组件。
示例:
yaml
# application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
框架会自动把以上配置绑定到 DataSourceProperties,再供给数据源 Bean 使用。
#mermaid-svg-MNAO76dfP1UuHoTe{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-MNAO76dfP1UuHoTe .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-MNAO76dfP1UuHoTe .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-MNAO76dfP1UuHoTe .error-icon{fill:#552222;}#mermaid-svg-MNAO76dfP1UuHoTe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MNAO76dfP1UuHoTe .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-MNAO76dfP1UuHoTe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MNAO76dfP1UuHoTe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MNAO76dfP1UuHoTe .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-MNAO76dfP1UuHoTe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MNAO76dfP1UuHoTe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MNAO76dfP1UuHoTe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MNAO76dfP1UuHoTe .marker.cross{stroke:#333333;}#mermaid-svg-MNAO76dfP1UuHoTe svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MNAO76dfP1UuHoTe p{margin:0;}#mermaid-svg-MNAO76dfP1UuHoTe .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-MNAO76dfP1UuHoTe .cluster-label text{fill:#333;}#mermaid-svg-MNAO76dfP1UuHoTe .cluster-label span{color:#333;}#mermaid-svg-MNAO76dfP1UuHoTe .cluster-label span p{background-color:transparent;}#mermaid-svg-MNAO76dfP1UuHoTe .label text,#mermaid-svg-MNAO76dfP1UuHoTe span{fill:#333;color:#333;}#mermaid-svg-MNAO76dfP1UuHoTe .node rect,#mermaid-svg-MNAO76dfP1UuHoTe .node circle,#mermaid-svg-MNAO76dfP1UuHoTe .node ellipse,#mermaid-svg-MNAO76dfP1UuHoTe .node polygon,#mermaid-svg-MNAO76dfP1UuHoTe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-MNAO76dfP1UuHoTe .rough-node .label text,#mermaid-svg-MNAO76dfP1UuHoTe .node .label text,#mermaid-svg-MNAO76dfP1UuHoTe .image-shape .label,#mermaid-svg-MNAO76dfP1UuHoTe .icon-shape .label{text-anchor:middle;}#mermaid-svg-MNAO76dfP1UuHoTe .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-MNAO76dfP1UuHoTe .rough-node .label,#mermaid-svg-MNAO76dfP1UuHoTe .node .label,#mermaid-svg-MNAO76dfP1UuHoTe .image-shape .label,#mermaid-svg-MNAO76dfP1UuHoTe .icon-shape .label{text-align:center;}#mermaid-svg-MNAO76dfP1UuHoTe .node.clickable{cursor:pointer;}#mermaid-svg-MNAO76dfP1UuHoTe .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-MNAO76dfP1UuHoTe .arrowheadPath{fill:#333333;}#mermaid-svg-MNAO76dfP1UuHoTe .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-MNAO76dfP1UuHoTe .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-MNAO76dfP1UuHoTe .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MNAO76dfP1UuHoTe .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-MNAO76dfP1UuHoTe .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MNAO76dfP1UuHoTe .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-MNAO76dfP1UuHoTe .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-MNAO76dfP1UuHoTe .cluster text{fill:#333;}#mermaid-svg-MNAO76dfP1UuHoTe .cluster span{color:#333;}#mermaid-svg-MNAO76dfP1UuHoTe div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-MNAO76dfP1UuHoTe .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-MNAO76dfP1UuHoTe rect.text{fill:none;stroke-width:0;}#mermaid-svg-MNAO76dfP1UuHoTe .icon-shape,#mermaid-svg-MNAO76dfP1UuHoTe .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MNAO76dfP1UuHoTe .icon-shape p,#mermaid-svg-MNAO76dfP1UuHoTe .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-MNAO76dfP1UuHoTe .icon-shape .label rect,#mermaid-svg-MNAO76dfP1UuHoTe .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MNAO76dfP1UuHoTe .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-MNAO76dfP1UuHoTe .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-MNAO76dfP1UuHoTe :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} application.yml / properties
Binder 属性绑定器
@ConfigurationProperties
DataSourceProperties
DataSourceAutoConfiguration
@Bean dataSource()
DataSource Bean
Spring IOC 容器
步骤 8:最终结果
经过以上全部流程:
- 项目引入什么 Starter,对应 Class 存在,对应自动配置类就生效。
- 配置文件写什么参数,组件就使用什么属性。
- 用户手动定义同类型 Bean 时,优先使用自定义 Bean,覆盖默认配置。
- 最终所有框架组件,例如数据源、MVC、Redis、Tomcat 等,自动装配到 IOC 容器。
三、完整时序串联
一句话串联全流程:
SpringApplication.run()启动 → 解析@SpringBootApplication→ 激活@EnableAutoConfiguration→ 通过@Import载入AutoConfigurationImportSelector→ 读取spring.factories/AutoConfiguration.imports获取全量自动配置类 → 处理排除项、去重 → 遍历配置类并校验条件注解,筛选出环境匹配的类 → 解析@Configuration配置类与内部@Bean→ 通过@ConfigurationProperties绑定外部配置 → 实例化组件并注册到 IOC 容器,自动装配完成。
四、核心角色总结(记忆要点)
| 组件 / 注解 | 核心作用 |
|---|---|
@SpringBootApplication |
总入口,三合一注解。 |
@EnableAutoConfiguration |
自动装配总开关。 |
AutoConfigurationImportSelector |
自动配置类加载与筛选核心处理器。 |
spring.factories / AutoConfiguration.imports |
存储所有候选自动配置类清单。 |
@Conditional 系列注解 |
按需装配的核心,实现"有依赖才生效"。 |
XXXAutoConfiguration |
自动配置载体,本质是标准 @Configuration 类。 |
@ConfigurationProperties |
绑定 application 外部配置。 |
@ConditionalOnMissingBean |
保证用户 Bean 优先。 |
五、关键特性补充
| 特性 | 说明 |
|---|---|
| 延迟导入 | DeferredImportSelector 保证先扫描业务 Bean,再执行自动配置,使自定义 Bean 可以优先覆盖框架 Bean。 |
| Starter 与自动装配关系 | Starter 只负责引入依赖 jar;真正的自动装配逻辑在 spring-boot-autoconfigure 包中。 |
| 版本变更 | Spring Boot 2.7 后移除 spring.factories 自动配置项,改用 AutoConfiguration.imports,加载逻辑不变。 |
| 优势本质 | 利用 SPI + 条件注解实现约定大于配置,大幅减少手动配置。 |