我如何获取当前已解析的Spring Boot应用程序属性列表?

huangapple go评论121阅读模式
英文:

How can I get list of currently resolved Spring Boot application properties?

问题

在Spring Boot中,我们可以使用执行器(actuator)暴露两个端点,它们是:

  • configprops
  • env

第一个端点返回所有@ConfigurationProperties类的扫描结果,而第二个端点返回环境对象快照。

如果我想要查看当前已解析的Spring Boot属性在当前启用的配置文件下,例如在运行时调用:

val baz = environment.getProperty("foo")

假设foo可能来自N个源:环境变量、引导(bootstrap)、Spring Cloud配置等等。

但在应用程序的特定状态下,我们实际上不知道已解析哪个值,通常我们是基于文档的知识来假设(通常我们不会重复它们,因为我们不想考虑优先级的顺序)。

我该如何做呢?

最好的方法是拥有一个执行器端点,用于解析所有当前活动的属性。

另一个实际示例可能是:

  • 我忘记了一个属性
  • 我想从当前应用程序运行时获取提示,就像在IntelliJ Idea中一样,但在运行时
  • 我列出当前属性,找到我的
  • 现在我可以进行更改
英文:

In Spring Boot we can expose with the actuator two endpoints with the properties:

  • configprops
  • env

我如何获取当前已解析的Spring Boot应用程序属性列表?

The first returns a scan of all the @ConfigurationProperties classes and the second returns the environment object snapshot.

What if I want to see currently resolved spring boot properties under the currently enabled profiles, for example as I would call in runtime:

val baz = environemnet.getProperty("foo")

Assuming foo can be in N sources: environment variables, bootstrap, spring cloud config etc.

But in a particular state of the application we actually do not know which value has been resolved, we always assume based on our knowledge of the documentation (and usually we do not duplicate them because we do not want to think about the order of precedence).

How can I do it?

The best would be to have an actuator endpoint that resolves all the currently active properties.

Another practical example would be:

  • I do not remember a property

  • I want to get a hint from the current application runtime as we do in IntelliJ Idea, but in the runtime

  • I list current, find my

  • I can change it now

答案1

得分: 1

The env endpoint可以返回单个属性的详细信息。对/actuator/env/fooGET请求将告诉您foo的值,找到该值的属性源以及有关其他可能存在foo但优先级较低的属性源的信息。

我认为值得注意的是/actuator/env按优先级顺序返回属性源。您可以通过在客户端端对响应进行一些处理来创建属性及其值的列表。按顺序遍历属性源,并仅在尚未看到属性时添加属性。

英文:

The env endpoint can return details for an individual property. A GET request to /actuator/env/foo will tell you the value of foo, the property source in which the value was found, and information about the other property sources where foo may exist but had lower precedence.

I think it's also worth noting that /actuator/env returns the property sources in order of precedence. You could create the list of properties and their values with some processing of the response on the client side. Iterate through the property sources in order and only add a property if it hasn't already been seen.

huangapple
  • 本文由 发表于 2023年8月10日 21:51:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定