Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing all properties set via Spring PropertyPlaceholderConfigurer

Tags:

java

spring

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

like image 807
Darth Ninja Avatar asked Nov 28 '11 15:11

Darth Ninja


People also ask

What is propertyplaceholderconfigurer in Spring Boot?

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}.

How to set the location of the property files in propertyplaceholderconfigurer?

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.

How to resolve the place holder of a property in Spring container?

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.

What is propertyplaceholderconfigurer in Salesforce?

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.


2 Answers

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();
        }
    }
}
like image 200
fl4l Avatar answered Nov 07 '22 03:11

fl4l


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.

like image 42
Dave Newton Avatar answered Nov 07 '22 02:11

Dave Newton