Contextual Path Based External Configuration

I would like to deploy multiple independent copies of a specific web application on the same tomcat server under different context paths. Each web application will need different configuration parameters (database name, password, etc.), but I would like the wars to be the same.

My plan was for the application to launch its context path at startup, and then read the specific .properties file outside of tomcat, identified by the context path. For example, if the war was deployed to {tomcat path} / webapps / pineapple, then I would like to read /config/pineapple.properties

I tried to find a way to load an instance of ServletContext through spring (3), but all the tips I have seen so far use the deprecated ServletContextFactoryBean.

Is there a better way to get the entered context path or a better way to load external files based on the context path?

+4
source share
3 answers
  • Extend Propertyplaceholderconfigurer to use the database to get the values. Example here
  • Load the actual settings (database name, password, etc.) into db as part of the seed data
  • When your ctx web application initializes, properties are resolved from the database

This is the approach we followed and it works great. If you can upgrade to Spring 3.1, then it supports Environment Profiles , which may be useful to you.

+1
source

Using ServletContextAttributeFactoryBean and Spring EL, you can refer to ServletContext initialization parameters ( <context-param> in web.xml) as follows:

 #{contextAttributes.myKey} 

This allows you to use PropertyPlaceHolderConfigurer and load property files from arbitrary user locations:

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="#{contextParameters.APP_HOME}/conf/app.properties"/> </bean> 

Corresponding ServletContext init parameter definition in Tomcat context.xml:

 <Parameter name="APP_HOME" value="file:/test" override="false"/> 

Or in your web.xml application:

 <context-param> <param-name>APP_HOME</param-name> <param-value>file:/test</param-value> </context-param> 
+4
source

It must be a solution.

 <bean name="envConfig" class="EnvironmentConfiguration"> <property name="locations"> <list> <value>file:///#{servletContext.contextPath}.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true" /> </bean> 
+4
source

All Articles