全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-18 5 分钟 ✍️ juanwangdev

Spring Boot spring.factories配置

spring.factories是Spring Boot SPI机制的核心文件,用于注册各种扩展组件。

文件位置

properties
src/main/resources/META-INF/spring.factories

文件格式

properties
# Key: 接口/注解全限定名
# Value: 实现类全限定名,多个用逗号分隔,可跨行用\续行

接口全限定名=\
实现类1,\
实现类2

自动配置注册

properties
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.autoconfigure.MyAutoConfiguration,\
com.example.autoconfigure.AnotherAutoConfiguration

监听器注册

Java
# ApplicationListener
org.springframework.context.ApplicationListener=\
com.example.listener.MyApplicationListener

# SpringApplicationRunListener
org.springframework.boot.SpringApplicationRunListener=\
com.example.listener.MyRunListener
properties
public class MyApplicationListener implements ApplicationListener<ApplicationStartedEvent> {

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        System.out.println("应用启动完成");
    }
}

初始化器注册

Java
org.springframework.context.ApplicationContextInitializer=\
com.example.initializer.MyApplicationContextInitializer
properties
public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext context) {
        // 在ApplicationContext刷新前执行初始化
        context.getEnvironment().getPropertySources().addFirst(
            new MapPropertySource("custom", Collections.singletonMap("key", "value")));
    }
}

EnvironmentPostProcessor

Java
org.springframework.boot.env.EnvironmentPostProcessor=\
com.example.env.MyEnvironmentPostProcessor
properties
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment,
                                       SpringApplication application) {
        // 在Environment准备完成后、ApplicationContext刷新前执行
        // 可添加自定义PropertySource
        environment.getPropertySources().addLast(
            new MapPropertySource("myConfig", Map.of("my.key", "my.value")));
    }
}

BeanPostProcessor

properties
org.springframework.beans.factory.config.BeanPostProcessor=\
com.example.processor.MyBeanPostProcessor

自动配置排除

properties
org.springframework.boot.autoconfigure.EnableAutoConfiguration.exclude=\
com.example.autoconfigure.ExcludedAutoConfiguration

Spring Boot 2.7+ 新规范

新文件路径

properties
# META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
# 每行一个配置类,无需key-value格式
com.example.autoconfigure.MyAutoConfiguration
com.example.autoconfigure.AnotherAutoConfiguration

自动配置排除

Java
# META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.excludes
com.example.autoconfigure.ExcludedAutoConfiguration

兼容性

text
// Spring Boot 2.7-3.0 同时支持新旧两种方式
// 优先读取新格式文件
// 建议新项目使用新格式

多模块配置

text
my-module-a/
└── META-INF/spring.factories  # 注册ModuleA配置

my-module-b/
└── META-INF/spring.factories  # 注册ModuleB配置

# SpringFactoriesLoader扫描所有jar包中的spring.factories
# 按接口分类合并所有实现类

常见注册类型

类型接口Key
自动配置EnableAutoConfiguration
应用监听器ApplicationListener
上下文初始化器ApplicationContextInitializer
环境后处理器EnvironmentPostProcessor
Bean后处理器BeanPostProcessor
Failure分析器FailureAnalyzer

要点总结

  • spring.factories是SPI机制核心文件
  • 格式为Properties,key为接口全限定名
  • 支持注册自动配置、监听器、初始化器等
  • 多个实现类用逗号分隔
  • Spring Boot 2.7+推荐新imports文件格式

📝 发现内容有误?点击此处直接编辑

← 上一篇 Spring Boot 随机值与占位符
下一篇 → Spring Boot 自定义Starter命名规范
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库