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

如何获取Spring Boot应用当前已解析的配置属性列表?

如何在Spring Boot运行时查看并管理最终生效的配置属性

现有Actuator端点的局限

Spring Boot Actuator提供了两个和配置相关的端点,但都无法直接满足查看最终生效属性的需求:

  • configprops:仅返回所有@ConfigurationProperties注解类的扫描结果,不是全局生效属性
  • env:返回环境对象的快照,但包含所有属性源的原始数据,无法直接拿到经过优先级解析后的最终值

而我们需要的是和调用以下代码完全一致的结果——直接获取当前启用配置下,经过多来源(环境变量、bootstrap配置、Spring Cloud Config等)优先级解析后的最终属性值:

val baz = environment.getProperty("foo")

典型需求场景

  • 记不清目标属性的完整名称,需要从运行时获取属性列表提示
  • 想确认某个属性的最终生效值,避免因优先级问题重复配置
  • 希望在运行时直接修改属性值并生效

解决方案:自定义Actuator端点

我们可以通过自定义Actuator端点,直接基于Spring的Environment获取最终生效属性,同时支持属性修改。

1. 依赖准备

确保项目引入Spring Boot Actuator依赖:
Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Gradle(Kotlin DSL):

implementation("org.springframework.boot:spring-boot-starter-actuator")

2. 实现自定义端点

创建端点类,注入Environment和ConfigurableApplicationContext来处理属性的读取与修改:

import org.springframework.boot.actuate.endpoint.annotation.Endpoint
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource
import org.springframework.core.env.ConfigurableEnvironment
import org.springframework.core.env.MutablePropertySources
import org.springframework.stereotype.Component
import org.springframework.context.ConfigurableApplicationContext
import java.util.TreeMap

@Component
@Endpoint(id = "resolvedProperties")
class ResolvedPropertiesEndpoint(
    private val environment: ConfigurableEnvironment,
    private val applicationContext: ConfigurableApplicationContext
) {

    // 读取所有最终生效的属性,按名称排序
    @ReadOperation
    fun getAllResolvedProperties(): Map<String, Any?> {
        val resolvedProps = TreeMap<String, Any?>()
        
        // 遍历所有属性源,用environment.getProperty获取最终生效值(自动处理优先级)
        environment.propertySources.forEach { source ->
            if (source is MapConfigurationPropertySource) {
                source.source.forEach { (key, _) ->
                    resolvedProps[key] = environment.getProperty(key)
                }
            }
        }
        return resolvedProps
    }

    // 修改指定属性的值(支持动态刷新场景)
    @WriteOperation
    fun updateProperty(key: String, value: String): Map<String, String> {
        // 添加最高优先级的临时属性源覆盖原有值
        val overrideSource = MapConfigurationPropertySource(mutableMapOf(key to value))
        (environment.propertySources as MutablePropertySources).addFirst(overrideSource)
        
        // 触发配置刷新(需引入Spring Cloud Refresh依赖才生效)
        runCatching {
            applicationContext.getBean("refreshEndpoint").javaClass.getMethod("refresh").invoke(null)
        }
        
        return mapOf("status" to "success", "key" to key, "newValue" to value)
    }
}

3. 暴露自定义端点

在配置文件中开启并暴露自定义端点:

management:
  endpoints:
    web:
      exposure:
        include: resolvedProperties,configprops,env
  endpoint:
    resolved-properties:
      enabled: true

4. 使用方式

  • 查看所有生效属性:访问/actuator/resolvedProperties,返回按名称排序的所有最终生效属性键值对
  • 修改属性:发送POST请求到/actuator/resolvedProperties,携带key和value参数,即可修改指定属性(需动态刷新的属性需配合Spring Cloud Refresh,静态属性仍需重启应用)

补充说明

  • 该端点返回的属性值和environment.getProperty(key)的结果完全一致,自动处理了Spring Boot的属性优先级规则
  • 可根据需求在getAllResolvedProperties方法中添加过滤逻辑(比如只保留特定前缀的属性)
  • 对于敏感属性(如密码),可在端点中添加脱敏逻辑避免信息泄露

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 22:24:55