You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring Boot应用启动时记录所有属性?

如何在Spring Boot启动阶段(自动配置前)记录所有属性

针对你遇到的「自动配置器运行时无法查看属性」的问题,比如HikariCP初始化崩溃时的属性排查,以下是几个直接可行的方案:

方案1:自定义SpringApplicationRunListener

SpringApplicationRunListener可以监听启动全生命周期,其中environmentPrepared阶段是环境准备完成、自动配置开始前的关键节点,刚好适合输出所有属性。

  • 步骤1:实现Listener接口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;

import java.util.Iterator;

public class EnvLoggingRunListener implements SpringApplicationRunListener {

    // 必须保留该构造方法,Spring Boot通过反射实例化
    public EnvLoggingRunListener(SpringApplication application, String[] args) {}

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        System.out.println("=== 启动阶段全量环境属性 ===");
        Iterator<PropertySource<?>> iterator = environment.getPropertySources().iterator();
        while (iterator.hasNext()) {
            PropertySource<?> source = iterator.next();
            System.out.printf("PropertySource: %s%n", source.getName());
            // 仅遍历可枚举的属性源,避免UnsupportedOperationException
            if (source instanceof org.springframework.core.env.MapPropertySource mapSource) {
                mapSource.getSource().forEach((key, value) -> {
                    // 敏感字段脱敏
                    if (key.contains("password") || key.contains("secret") || key.contains("token")) {
                        System.out.printf("  %s: ******%n", key);
                    } else {
                        System.out.printf("  %s: %s%n", key, value);
                    }
                });
            }
        }
    }

    // 其余生命周期方法留空即可
    @Override public void starting() {}
    @Override public void contextPrepared(ConfigurableApplicationContext context) {}
    @Override public void contextLoaded(ConfigurableApplicationContext context) {}
    @Override public void started(ConfigurableApplicationContext context) {}
    @Override public void running(ConfigurableApplicationContext context) {}
    @Override public void failed(ConfigurableApplicationContext context, Throwable exception) {}
}
  • 步骤2:注册Listener
    在项目src/main/resources/META-INF/spring.factories文件中添加:
org.springframework.boot.SpringApplicationRunListener=com.yourpackage.EnvLoggingRunListener

方案2:使用EnvironmentPostProcessor

这是Spring Boot专门用于扩展环境配置的扩展点,执行时机比Listener的environmentPrepared更早,同样在自动配置启动前完成属性输出。

  • 步骤1:实现PostProcessor接口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;

import java.util.Iterator;

public class EnvLoggingPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        System.out.println("=== EnvironmentPostProcessor输出全量属性 ===");
        Iterator<PropertySource<?>> iterator = environment.getPropertySources().iterator();
        while (iterator.hasNext()) {
            PropertySource<?> source = iterator.next();
            System.out.printf("PropertySource: %s%n", source.getName());
            if (source instanceof org.springframework.core.env.MapPropertySource mapSource) {
                mapSource.getSource().forEach((key, value) -> {
                    if (key.matches(".*(password|secret|key|token).*")) {
                        System.out.printf("  %s: ******%n", key);
                    } else {
                        System.out.printf("  %s: %s%n", key, value);
                    }
                });
            }
        }
    }
}
  • 步骤2:注册PostProcessor
    同样在src/main/resources/META-INF/spring.factories中添加:
org.springframework.boot.env.EnvironmentPostProcessor=com.yourpackage.EnvLoggingPostProcessor

方案3:调整日志配置(快速排查用)

如果不想写代码,可以通过调整日志级别让Spring Boot输出配置加载细节:
在application.yml中添加:

logging:
  level:
    org.springframework.boot.context.config.ConfigDataPropertySource: TRACE
    org.springframework.boot.autoconfigure: DEBUG

这个方案会输出配置加载的过程信息,但不如自定义扩展点直观汇总所有属性,适合快速定位单类配置问题。

注意事项

  • 敏感信息:必须对密码、密钥等字段做脱敏处理,避免泄露。
  • 性能:遍历全量属性会增加启动耗时,仅在调试阶段使用,上线前务必移除或禁用。
  • 属性优先级:遍历PropertySources的顺序就是属性优先级顺序(命令行参数 > 环境变量 > application配置文件等),靠前的源优先级更高。

内容的提问来源于stack exchange,提问作者gabriel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 06:18:27