问题描述
我们的操作人员希望在应用启动时将Spring引导配置(即所有属性)转储到日志文件中。我认为可以通过使用注解@ConfigurationProperties注入属性并打印它们来完成此操作。
Our Ops guys want the Spring boot configuration (i.e. all properties) to be dumped to the log file when the app starts. I assume this can be done by injecting the properties with annotation @ConfigurationProperties and printing them.
问题是是否有更好的或内置的机制来实现这一目标。
The questions is whether there is a better or built-in mechanism to achieve this.
鉴于似乎没有内置的解决方案,我尝试自己做饭。这是我想出的:
Given there does not seem to be a built in solution besides, I was try to cook my own. Here is what I came up with:
@Component
public class ConfigurationDumper {
@Autowired
public void init(Environment env){
log.info("{}",env);
}
}
挑战是它不打印我的application.yml中的变量。相反,这是我得到的:
The challenge with this is that it does not print variables that are in my application.yml. Instead, here is what I get:
StandardServletEnvironment
{
activeProfiles=[],
defaultProfiles=[default],
propertySources=[
servletConfigInitParams,
servletContextInitParams,
systemProperties,
systemEnvironment,
random,
applicationConfig: [classpath: /application.yml]
]
}
我该如何要解决此问题,以便加载和打印所有属性?
How can I fix this so as to have all properties loaded and printed?
推荐答案
如果使用, env
端点将为您提供所有在 ConfigurableEnvironment
和 configprops
中设置的配置属性将为您提供 @ConfigurationProperties
,但不在日志中。
If you use actuator , env
endpoint will give you all the configuration properties set in ConfigurableEnvironment
and configprops
will give you the list of @ConfigurationProperties
, but not in the log.
看看,也许是它将使您了解如何获取所有感兴趣的属性。
Take a look at the source code for this env
endpoint, may be it will give you an idea of how you could get all the properties you are interested in.
这篇关于转储Spring Boot配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!