Tuesday, June 25, 2013

Externalize Properties File in a Java Web Application

To be able to load properties file from a file system rather than bundling it with your web application you should do the following:

   1.      Define “application base” or “property file location” as a context parameter in web.xml
  <context-param>
      <param-name>app.base</param-name>
      <param-value>file:///c:/my/app</param-value>
  </context-param>



            2.      Load all properties file using Spring’s PropertyPlaceholderConfigurer.
 
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
    <list>
         <value>${app.base}/myapp-app.properties</value>
         <value>${app.base}/myapp-db.properties</value>
    </list>
</property>
</bean>


If you know any other way of doing it, please leave a comment.