0

I am trying to set the properties file inside a class that extends the PropertyPlaceholderConfigurer based upon the environment (local, dev, ref, qa, prod)

My folder structure looks like the following.

properties
   environment.properties
   server-local.properties
   server-ref.properties
   server-prod.properties
   email-local.properties
   email-ref.properties
   email-prod.properties
   cache-local.properties
   cache-ref.properties
   cache-prod.properties

The environment.properties has a property

environment.stage=local  (or whatever env this is)

My Spring Integration context statements look something like this:

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

<bean id="propertyPlaceholder" class="com.turner.bvi.BviPropertiesUtil">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="locations">
        <list>
            <value>classpath:properties/environment.properties</value>
            <value>classpath:properties/*-${environment.stage}.properties</value>
        </list>
    </property>

</bean>

What I want to do is have only the properties file from the particular environment stage load (be it local, ref, prod .... etc.). How do I get just this second set of properties files to load based upon environment.stage?

Thanks for the help in advance.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Is this a web application? If you are using an application server like Tomcat, you can set _environment.stage_ as an environment variable and read the value to determine which properties file to select. The following link should be helpful in showing you methods of reading the environment variables with Spring: [how to read System environment variable in Spring applicationContext](http://stackoverflow.com/questions/3965446/how-to-read-system-environment-variable-in-spring-applicationcontext) – M7Jacks Aug 26 '14 at 20:01
  • No this isn't a web application. I'm trying to get those properties files related to the particular environment. Getting the "environment.stage" variable is not the problem. That works well. Getting the files that have that environment.stage in their name is the problem. :( I am trying to use the "*-${environment.stage}" statement to try this. Am I TOTALLY on the wrong track? Spring cannot resolve that path. Maybe using that in the tag doesn't work. – Stephen McConnell Aug 26 '14 at 20:13
  • After reading the article you posted, I think I may be able to create a util properties tag in the context and then use a "ref" as a reference to that id.... Gonna try it now. – Stephen McConnell Aug 26 '14 at 20:21

2 Answers2

0

You can use Spring profiles for this, it would be something like this:

<context:property-placeholder location="classpath:properties/*.properties" />
<beans profile="local">

    <bean id="propertyPlaceholder" class="com.turner.bvi.BviPropertiesUtil">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="locations">
            <list>
                <value>classpath:properties/environment.properties</value>
                <value>classpath:properties/*-local.properties</value>
            </list>
        </property>
    </bean>
</beans>

<beans profile="dev">
    <bean id="propertyPlaceholder" class="com.turner.bvi.BviPropertiesUtil">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="locations">
            <list>
                <value>classpath:properties/environment.properties</value>
                <value>classpath:properties/*-local.properties</value>
            </list>
        </property>
    </bean>
</beans>
...

Setting an environment variable can be done using spring_profiles_active (or spring_profiles_default). In Unix try exporting SPRING_PROFILES_DEFAULT=local

You can alternativerly use the JVM parameters like -Dspring.profiles.active=local

In case you need to maintain the environment variables as they are, you can implement a custom ActiveProfilesResolver as described in here: Spring Profiles: Simple example of ActiveProfilesResolver?

Community
  • 1
  • 1
Adrian Lopez
  • 1,776
  • 1
  • 17
  • 35
  • Thank you very much. This is a good solution, but I was also able to accomplish the purpose using environment variables set through Chef or through creating a Docker container with the environment variables in it. See the post below. – Stephen McConnell Aug 27 '14 at 16:09
0

Thank you all for your help. I was able to take snippets of all your suggestions and come up with a solution using "environmentProperties" in the Spring context.

The problem with trying to use a in the context was that it had not been set at the time MY class was trying to resolve the ${environment.stage}... Or at least that is what I gathered from searching other posts.

If my properties file structure looks like this:

properties
   environment.properties
   server-local.properties
   server-ref.properties
   server-prod.properties
   email-local.properties
   email-ref.properties
   email-prod.properties
   cache-local.properties
   cache-ref.properties
   cache-prod.properties

I was able to set the Environment property 'env' to the correct value through Chef or through a Docker container and use the following code.

<!-- Register the properties file location -->
<context:property-placeholder location="classpath:properties/*.properties" />

<bean id="propertyPlaceholder" class="com.turner.bvi.BviPropertiesUtil">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="locations">
       <list>
           <value>classpath:properties/environment.properties</value>
           <value>classpath:properties/*-#{systemEnvironment['env']}.properties</value>
       </list>
    </property>
</bean>

I could have put each of the property sets under its own directory and had

<value>classpath:properties/#{systemEnvironment['env']}/*.properties</value>

Again, thank you all for your help.