Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring loading application.properties based on tomcat servlet context definition

I need to have a development and production settings for our spring project. I understand that you can use profiles for spring but that is not something that we can do.

What I want to do is place on the development environment a test-application.properties file and on production a prod-application.properties file. In the tomcat context definition we sent the following:

<Context>
    <context-param>
        <param-name>properties_location</param-name>
        <param-value>file:C:\Users\Bill\test-application.properties</param-value>
    </context-param>
</Context>

And we can have the value changed for the production servers. In the spring config we have something like this:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>${properties_location}</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="false" />
</bean>

But we keep getting errors like:

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/${properties_location}]

Any ideas on how to solve?

like image 941
checklist Avatar asked Jul 31 '12 07:07

checklist


1 Answers

One feature of PropertyPlaceholder is that you can define multiple resource locations. So for example you can define your-production-config.properties along with file:C:/Users/${user.name}/test-application.properties

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:your-production-config.properties</value>
            <value>file:C:/Users/${user.name}/test-application.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>        
</bean>

for production you need to place prod configuration into classpath somewhere(really not important where exactly, just classpath) - for local env you can use convension like this file:C:/Users/${user.name}/test-application.properties

like image 73
Andrey Borisov Avatar answered Nov 13 '22 11:11

Andrey Borisov