SpringBoot的自动装配原理
Spring Boot 的自动装配(Auto-Configuration)是其核心特性之一,它极大地简化了Spring应用的配置过程。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootLearnApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootLearnApplication.class, args);
}
}
1. 核心入口:@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
- 这是一个复合注解,包含:
- @SpringBootConfiguration:标识这是一个配置类
- @EnableAutoConfiguration:启用自动配置
- @ComponentScan:启用组件扫描
2. @EnableAutoConfiguration注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
/**
* Environment property that can be used to override when auto-configuration is
* enabled.
*/
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
Class<?>[] exclude() default {};
/**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
String[] excludeName() default {};
}
这个注解通过@Import引入了AutoConfigurationImportSelector类
- AutoConfigurationImportSelector 是核心类,负责加载自动配置类:
public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware,
ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {\
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
AnnotationAttributes attributes = getAttributes(annotationMetadata);
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
configurations = removeDuplicates(configurations);
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = getConfigurationClassFilter().filter(configurations);
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
}
3. 自动配置类加载流程
3.1 加载候选配置类
从 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件加载候选配置类(Spring Boot 2.7+ 后替代 spring.factories)
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
ImportCandidates importCandidates = ImportCandidates.load(this.autoConfigurationAnnotation,
getBeanClassLoader());
List<String> configurations = importCandidates.getCandidates();
Assert.notEmpty(configurations,
"No auto configuration classes found in " + "META-INF/spring/"
+ this.autoConfigurationAnnotation.getName() + ".imports. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
在 Spring Boot 3.x 中,自动配置类的加载方式从 spring.factories 过渡到 AutoConfiguration.imports,并引入了 ImportCandidates 类来处理这一变化。
3.2 通过条件注解过滤有效配置类
关键条件注解:
- @ConditionalOnClass:类路径存在指定类时生效
- @ConditionalOnMissingBean:容器中不存在指定 Bean 时生效
- @ConditionalOnProperty:配置属性匹配时生效
4. 实现一个自定义的Starter
4.1 新建一个maven项目
项目结构:
package org.example.springbootstarterdemo;
public class HelloWorldService {
private String message = "Hello from Starter!";
public void printMessage() {
System.out.println(message);
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package org.example.springbootstarterdemo;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class StarterAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public HelloWorldService helloWorldService() {
return new HelloWorldService();
}
}
org.springframework.boot.autoconfigure.AutoConfiguration.imports:
org.example.springbootstarterdemo.StarterAutoConfiguration
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>simple-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
# 在项目根目录执行
mvn clean install
4.2 SpringBoot 项目引入
在pom.xml添加
<dependency>
<groupId>org.example</groupId>
<artifactId>simple-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
package org.example.springbootlearn;
import org.example.springbootstarterdemo.HelloWorldService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringBootLearnApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringBootLearnApplication.class, args);
HelloWorldService bean = context.getBean(HelloWorldService.class);
System.out.println(bean.getMessage());
}
}
启动时添加 --debug 参数:
StarterAutoConfiguration#helloWorldService matched:
- @ConditionalOnMissingBean (types: org.example.springbootstarterdemo.HelloWorldService; SearchStrategy: all) did not find any beans (OnBeanCondition)
总结
Spring Boot 的自动装配通过以下步骤实现:
-
触发入口:@EnableAutoConfiguration 注解激活自动配置机制
-
加载候选:通过 AutoConfiguration.imports 文件加载所有候选配置类
-
条件过滤:利用 @Conditional 系列注解筛选有效配置
-
Bean注册:符合条件的配置类通过 @Bean 方法注册组件
-
动态适配:根据类路径、配置属性等环境因素动态调整最终配置
通过这种机制,Spring Boot 实现了 "约定大于配置" 的设计理念,显著减少了手动配置的工作量。