如何在SpringBoot应用中获取所有已加载配置文件的键值对
Spring Boot 输出所有加载的配置键值对方案
针对多目录配置、多Profile场景下的配置优先级疑问,以下是几种可行的实现方案,能在应用启动时输出所有读取的配置文件的全部键值对:
方案一:开启Spring Boot调试日志
直接通过启动参数开启调试模式,Spring Boot会自动输出配置加载的完整细节,包括每个配置文件的加载顺序、配置键的来源及覆盖情况:
- 启动时添加参数:
--debug(或VM参数:-Ddebug) - 日志中会包含:
- 所有加载的配置文件路径(包括通过
spring.config.additional-location指定的外部目录) - 每个配置键的具体来源文件
- 配置键被覆盖的记录(如
Overriding property 'xxx' with value 'yyy' from location '...')
- 所有加载的配置文件路径(包括通过
方案二:自定义监听器输出所有配置源
通过实现ApplicationListener监听应用启动完成事件,遍历所有配置源并输出键值对,可精准过滤配置文件相关的来源:
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertySource; import org.springframework.stereotype.Component; @Component public class FullConfigPrinter implements ApplicationListener<ApplicationReadyEvent> { @Override public void onApplicationEvent(ApplicationReadyEvent event) { ConfigurableEnvironment env = event.getApplicationContext().getEnvironment(); System.out.println("===== 所有加载的配置文件及键值对 ====="); for (PropertySource<?> source : env.getPropertySources()) { // 只过滤配置文件类型的来源(排除系统属性、环境变量等) if (source.getName().startsWith("applicationConfig:")) { System.out.printf("\n--- 配置文件:%s ---\n", source.getName().replace("applicationConfig: [", "").replace("]", "")); if (source.getSource() instanceof java.util.Map) { ((java.util.Map<?, ?>) source.getSource()).forEach((key, value) -> System.out.printf("%s=%s\n", key, value) ); } } } System.out.println("===== 配置输出结束 ====="); } }
- 该代码会在应用启动完成后,仅输出从
application.properties/application-{profile}.properties加载的所有键值对,同时标注对应的文件路径。 - 配置源的顺序就是Spring Boot的优先级顺序,越靠前的源优先级越高,后面的源会覆盖前面的同名配置键。
方案三:借助Spring Boot Actuator输出配置
如果你的应用已经集成了Actuator,可以在启动类中主动调用/env端点的逻辑来输出配置:
- 确保Actuator依赖已添加,并开启
env端点 - 注入
EnvironmentEndpoint,在应用启动时调用其environment()方法获取所有配置并输出:
import org.springframework.boot.actuate.env.EnvironmentEndpoint; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component public class ActuatorConfigPrinter implements ApplicationListener<ApplicationReadyEvent> { private final EnvironmentEndpoint environmentEndpoint; public ActuatorConfigPrinter(EnvironmentEndpoint environmentEndpoint) { this.environmentEndpoint = environmentEndpoint; } @Override public void onApplicationEvent(ApplicationReadyEvent event) { System.out.println("===== Actuator 输出所有配置 ====="); environmentEndpoint.environment().getPropertySources().forEach(source -> { if (source.getName().startsWith("applicationConfig:")) { System.out.printf("\n--- %s ---\n", source.getName()); source.getProperties().forEach((key, value) -> System.out.printf("%s=%s\n", key, value) ); } }); System.out.println("===== 配置输出结束 ====="); } }
- 这种方式会输出更结构化的配置信息,同时标注每个配置的来源,适合需要更规范输出的场景。
注意事项
- 若配置中包含敏感信息(如密码),输出前需做好脱敏处理,避免泄露。
- Spring Boot的配置优先级遵循官方规则:外部配置文件 > 内部配置文件,激活的Profile配置 > 默认配置。
内容的提问来源于stack exchange,提问作者Scala Enthusiast
相关产品推荐
相关产品推荐

