Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: Injecting different properties file according to profile

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?

like image 722
Joris Avatar asked Feb 24 '12 17:02

Joris


People also ask

Can we have multiple application properties in spring boot?

So you can create two applications. properties files one for the original database and the other for the test database which you use during development.

How do I keep my spring boot profiles different?

The solution would be to create more property files and add the "profile" name as the suffix and configure Spring Boot to pick the appropriate properties based on the profile. Then, we need to create three application. properties : application-dev.

What 2 types of file formats can use to inject properties into the spring environment object?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration.


1 Answers

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();
like image 79
tolitius Avatar answered Sep 17 '22 12:09

tolitius