Spring property-placeholder: External properties configuration

The PopertyPlaceholderConfigurer

A common way of setting the configuration of the web application is by defining it in a properties file. We can find this file in the classpath. The way of doing this is using the PropertyPlaceholderConfigurer class, or simplifying the configuration, using the context namespace property-placeholder.

That’s ok but, what if you need a different configuration for different environments? Or, once the web application is deployed on the server, what happens if we need to change some value? What we will have to do is modify the application, redeploy the war file and restart the server. If there’s only one application deployed, then it’s not much of a problem, but imagine a properties file which contains generic configuration that is shared by all the applications on the server.

Externalizing the properties file

By putting the properties file in the server file system, you will have your configuration outside the web application. In case you need to change a value, you will only need to modify the file and restart the server, without needing to redeploy the application.

Let’s assume that we put our properties file in the “D:environmentconfig” folder. What we need to do is add a new argument to the server start up script. If you use an IDE, for example Eclipse, double click the server name (servers tab) and click on the Open launch configuration link:

property-placeholder-launch-configuration

Then, select the arguments tab and add a new argument with the properties path as shown below:

vm arguments

Once we set the arguments, the Spring configuration with the property placeholder is as follows:

 

Share it:

6 thoughts on “Spring property-placeholder: External properties configuration

Leave a Reply

Your email address will not be published. Required fields are marked *