Spring property-placeholder: External properties configuration

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.

<context:property-placeholder location="datasource.properties"/>

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:


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:

<context:property-placeholder location="file:${databaseConfiguration}"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />