Monday, February 11, 2013

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:

6 comments:

  1. Replies
    1. Thank you for letting me know, Mauricio. It is now fixed.

      Delete
    2. Images are broken again for external users.

      Delete
    3. It seems there where some special chars in the HTML. I have rewritten the post so this shouldn't happen again. Thank you for your feedback!

      Delete