Spring: Injecting different properties file according to profile

12,589

A couple of options:


System Variables

You can use a prefix to control environment specific properties, this can be done by using system variables:

 <util:properties id="googleProperties" 
                  location="WEB-INF/${ENV_SYSTEM:dev}/google.properties" />

In this case it will always look under:

 <util:properties id="googleProperties" 
                  location="WEB-INF/dev/google.properties" />

by default, unless a ENV_SYSTEM system variable is set. If it is set to qa, for example, it will automatically look under:

 <util:properties id="googleProperties" 
                  location="WEB-INF/qa/google.properties" />

Spring Profiles

Another approach is to make beans profile specific. For example:

<beans profile="dev">
    <util:properties id="googleProperties" 
                     location="WEB-INF/google-dev.properties" />
</beans>

<beans profile="qa">
    <util:properties id="googleProperties" 
                     location="WEB-INF/google-qa.properties" />
</beans>

The appropriate googleProperties will loaded depending on a profile set. For example this will load WEB-INF/google-dev.properties:

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles( "dev" );
ctx.load( "classpath:/org/boom/bang/config/xml/*-config.xml" );
ctx.refresh();
Share:
12,589

Related videos on Youtube

Joris
Author by

Joris

Excited about solving real world challenges with others using computer technology. Love to explore new things. Lifelong learner. Want to see the world. Particularly fund of Java, Python and Javascript.

Updated on June 04, 2022

Comments

  • Joris
    Joris almost 2 years

    First, some context:

    I'm currently working on a project in which I use the Spring framework on Google's AppEngine (GAE) to fetch some data from one of Google's services. To do so, I make use of Google's OAuth facilities. For this, I need to use a clientSecret and clientId that are specific to my application. As these are static configuration values, I use Spring's <util:properties> (link to documentation) functionality to insert these values into the appropriate classes.

    XML config:

    <util:properties id="googleProperties" location="WEB-INF/google.properties" />
    

    Class usage:

    @Value("#{googleProperties['google.data.api.client.id']}")
    private String clientId;
    

    My Question:

    As it turns out, the values of clientId and clientSecret need to be different for production (when deployed on App Engine) as for development (on my local machine). In order to solve this without constantly needing to change the values in the properties file when deploying, I have been looking into Spring's configuration profiles that would allow me to specify different property files for production and for development. Although I have an idea how Spring profiles work based on the documentation, I am not at all sure what the appropriate solution would be in this particular situation.

    In other words, how can I inject different property files based on whether my application is deployed locally or on GAE?

  • Joris
    Joris about 12 years
    Ok, but how do I configure which profile should be used at which moment (preferably without manual editing every time)?
  • Joris
    Joris about 12 years
    Thanks! I ended up using your first solution in combination with GAE's com.google.appengine.runtime.environment system property: code.google.com/appengine/docs/java/…
  • Randell
    Randell over 8 years
    Where do you set the ENV_SYSTEM (when using Spring Tool Suite)?