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 which is located in the classpath. The way of doing this is using the PropertyPlaceholderConfigurer class, or simplifying the configuration, using the context namespace.

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 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:\environment\config\" 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:


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


Once done this, the Spring configuration with the property placeholder is as follows:

Labels: