I’d like to print the consolidated list of properties set in our application on startup. What is the best way to do this?
Thanks
The PropertyPlaceholderConfigurer tells the Spring IoC Container to load the property file which is present in the classpath and resolve any placeholders $ {….} In our JDBC_Example class bean we have replaced the hard coded jdbc details with the place holders like $ {jdbc.url},$ {jdbc.username} and $ {jdbc.password}.
The “locations” property of PropertyPlaceholderConfigurer can take the List of property files we simply need to give the location of all the files. Lets say we have two property files named ftp.properties and jdbc.properties, then the configuration will be like below.
This makes the spring container to resolve the place holders of both ftp.properties and jdbc.properties By default Spring IoC Container will be looking for the property file in the application directory (under src directory). You can have it under the sub folders as well, all you have to do is to prefix the property file with the location.
It can be local properties or system properties or environment variables. We can use PropertyPlaceholderConfigurer using XML as well as annotation. PropertyPlaceholderConfigurer externalizes the property configuration.
This is my implementation:
public class CustomPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer implements InitializingBean{
public void afterPropertiesSet(){
try{
Properties loadedProperties = this.mergeProperties();
for(Entry<Object, Object> singleProperty : loadedProperties.entrySet()){
logger.info("LoadedProperty: "+singleProperty.getKey()+"="+singleProperty.getValue());
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
Use a custom PropertyPlaceholderConfigurer
implementation that overrides the resolve...
methods and logs the placeholder
name. You may also need/want to override the convert...
methods, but resolve...
should handle it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With