一、核心功能与定义
spring.factories 是 Spring Boot 中基于 SPI(Service Provider Interface)机制的扩展配置文件,用于实现 自动化配置 和 模块化扩展。
核心作用:
加载自动配置类:将不在主应用包扫描路径下的类(如第三方库或自定义模块)注册到 Spring 容器。
扩展框架功能:支持动态注册监听器(ApplicationListener)、环境后处理器(EnvironmentPostProcessor)等组件。
解耦模块化开发:通过声明式配置实现不同模块的灵活集成,无需硬编码依赖。
二、工作原理
文件结构与加载机制
位置:必须位于项目的 META-INF 目录下,文件格式为键值对(key=value)。
加载流程:
Spring Boot 启动时,通过 SpringFactoriesLoader 类扫描所有依赖的 META-INF/spring.factories 文件。
解析键值对,反射实例化配置类并注入 Spring 容器。
条件加载机制
条件注解支持:如 @ConditionalOnClass、@ConditionalOnProperty 等,确保仅在满足条件时加载配置类。
优先级控制:通过 @Order 注解或实现 Ordered 接口,决定配置类的执行顺序。
三、常见配置项与示例
以下为 spring.factories 中常用的键及其作用:
键名 用途 示例
org.springframework.boot.autoconfigure.EnableAutoConfiguration 声明自动配置类,Spring Boot 启动时自动加载这些类。 com.example.MyAutoConfiguration
org.springframework.context.ApplicationContextInitializer 在 Spring 上下文初始化前执行自定义逻辑(如设置环境变量)。 com.example.MyContextInitializer
org.springframework.boot.env.EnvironmentPostProcessor 在环境准备阶段动态修改配置(如解密敏感信息)。 com.example.CustomEnvPostProcessor
org.springframework.context.ApplicationListener 注册事件监听器,响应 Spring 生命周期事件。 com.example.MyAppListener
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter 过滤不需要的自动配置类。 com.example.CustomImportFilter
示例文件内容:
# 自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.config.RedisAutoConfiguration,\
com.example.config.JpaAutoConfiguration
# 环境后处理器
org.springframework.boot.env.EnvironmentPostProcessor=\
com.example.processor.DecryptEnvPostProcessor
# 应用监听器
org.springframework.context.ApplicationListener=\
com.example.listener.CustomStartupListener
四、实际应用场景
自定义 Starter 开发
场景:开发通用组件(如数据库连接池、安全模块),供其他项目引入后自动配置。
实现:
在 Starter 的 spring.factories 中声明自动配置类。
其他项目引入依赖后,自动加载配置类并注册 Bean。
动态加载第三方库配置
场景:集成 MyBatis-Plus、Redis 等第三方库时,自动注入其配置类。
优势:无需手动 @Import 或修改主应用代码。
多环境配置适配
场景:根据运行环境(开发/生产)动态调整配置源。
实现:通过 EnvironmentPostProcessor 加载外部配置文件(如数据库配置)。
五、自定义springboot2.x的starter实例
1、项目结构与依赖
创建 Maven 项目
建议将 Starter 拆分为两个模块(非强制):
• my-starter-autoconfigure:存放自动配置逻辑
• my-starter:空模块,仅依赖 my-starter-autoconfigure
<!-- my-starter/pom.xml -->
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-starter-autoconfigure</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
配置自动配置模块
在 my-starter-autoconfigure 中添加核心依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.6.4</version>
</dependency>
</dependencies>
2、核心代码实现
定义属性配置类
通过 @ConfigurationProperties 绑定配置文件参数:
@ConfigurationProperties(prefix = "my.service")
@Data
public class MyServiceProperties {
private String message = "Default Message";
private int retryCount = 3;
}
编写服务逻辑
实现自定义功能的核心类:
public class MyService {
private final String message;
private final int retryCount;
public MyService(String message, int retryCount) {
this.message = message;
this.retryCount = retryCount;
}
public String execute() {
return "Processed: " + message + " (Retries: " + retryCount + ")";
}
}
创建自动配置类
使用条件注解控制 Bean 的创建逻辑:
@Configuration
@EnableConfigurationProperties(MyServiceProperties.class)
@ConditionalOnClass(MyService.class)
public class MyAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public MyService myService(MyServiceProperties properties) {
return new MyService(properties.getMessage(), properties.getRetryCount());
}
}
3、注册自动配置
配置 spring.factories
在 resources/META-INF 下创建文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.autoconfigure.MyAutoConfiguration
条件扩展(可选)
增加环境判断逻辑:
@Bean
@ConditionalOnProperty(name = "my.service.enabled", havingValue = "true")
public MyService advancedService() {
// 高级功能实现
}
4、打包与测试
安装到本地仓库
mvn clean install
在其他项目中引用
<dependency>
<groupId>com.example</groupId>
<artifactId>my-starter</artifactId>
<version>1.0.0</version>
</dependency>
配置示例
在 application.yml 中覆盖默认值:
my.service:
message: "Custom Starter Demo"
retryCount: 5
5、高级功能扩展
AOP 集成
@Aspect
public class MyServiceAspect {
@Around("execution(* com.example.MyService.*(..))")
public Object logExecution(ProceedingJoinPoint joinPoint) {
// 记录方法执行日志
}
}
多环境配置
通过 @Profile 注解实现环境隔离:
@Bean
@Profile("prod")
public MyService prodService() {
return new MyService("Production Mode", 10);
}
6、注意事项
命名规范
• 第三方 Starter 推荐使用 {yourname}-spring-boot-starter 格式
条件注解使用
• @ConditionalOnClass 确保依赖存在时加载配置
• @ConditionalOnMissingBean 避免重复注册 Bean
版本兼容性
• Spring Boot 2.x 需使用 JDK 8+,与 3.x 的 AutoConfiguration.imports 机制不兼容
通过以上步骤,可以快速实现一个支持动态配置、条件加载的 Spring Boot Starter。实际开发中建议参考 MyBatis-Plus 等成熟 Starter 的结构设计,提升模块的可维护性和扩展性。
六、与 Java SPI 的对比
特性 Spring Factories Java SPI
配置文件位置 META-INF/spring.factories META-INF/services/接口全限定名
功能扩展 支持自动配置、监听器、环境处理器等复杂逻辑。 仅支持接口实现类的加载。
条件加载 支持 @Conditional 系列注解。 不支持条件过滤。
应用场景 Spring Boot 生态的模块化扩展。 JDBC 驱动等基础服务发现。
六、注意事项与最佳实践
文件位置与命名规范
必须位于 src/main/resources/META-INF 目录下,文件名严格为 spring.factories。
避免配置冲突
多个依赖声明同名配置类时,使用 @Conditional 注解避免冲突。
性能优化
减少不必要的自动配置类加载,可通过 spring.autoconfigure.exclude 排除冗余配置。
总结
spring.factories 是 Spring Boot 自动配置与扩展的核心机制,通过声明式配置实现模块化开发与第三方库集成。掌握其原理与配置方法,能显著提升大型项目的灵活性和可维护性。实际开发中,建议结合条件注解和优先级控制,优化配置加载逻辑。
文章转载自soul0202,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




