14

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?

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
Joris
  • 3,220
  • 2
  • 23
  • 19

2 Answers2

13

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();
tolitius
  • 22,149
  • 6
  • 70
  • 81
  • Thanks! I ended up using your first solution in combination with GAE's com.google.appengine.runtime.environment system property: http://code.google.com/appengine/docs/java/runtime.html#The_Environment – Joris Feb 25 '12 at 04:06
  • Where do you set the ENV_SYSTEM (when using Spring Tool Suite)? – Randell Aug 20 '15 at 01:38
0

You are on the right track, in our application we have the same scenario and we use "profiles" to manage the properties. We use two configuration files one for Production and another one for testing with profiles set accordingly.

Sajan Chandran
  • 11,287
  • 3
  • 29
  • 38
  • Ok, but how do I configure which profile should be used at which moment (preferably without manual editing every time)? – Joris Feb 24 '12 at 17:49