Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dump Spring boot Configuration

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);
    }

}

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?

like image 428
Klaus Avatar asked Nov 19 '15 18:11

Klaus


2 Answers

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.

like image 67
jny Avatar answered Sep 28 '22 15:09

jny


There is no built-in mechanism and it really depends what you mean by "all properties". Do you want only the actual keys that you wrote or you want all properties (including defaults).

For the former, you could easily listen for ApplicationEnvironmentPreparedEvent and log the property sources you're interested in. For the latter, /configprops is indeed a much better/complete output.

like image 34
Stephane Nicoll Avatar answered Sep 28 '22 15:09

Stephane Nicoll