Spring 3.1 Environment Profile

The most useful addition to Spring 3.1 I’ve found is support for environments (called Profiles).

Here is the official post on this topic.


Profiles allow you to add / remove features / settings of your application based on the environment it is actually running in.


  <beans profile=“beta”>
<bean id=“appProperties” class=“wookets.bootstrap.AppProperties”>
<property name=“someprop” value=“CHEESE”/>
<beans profile=“local”>
<bean id=“appProperties” class=“wookets.bootstrap.AppProperties”>
<property name=“someprop” value=“WAFFLES”/>

If we are running the application on our beta server, we’ll get CHEESE.
If we are running the applciation on our local server, we’ll get WAFFLES.

But, how do we initially set the environment variable? There is a great post on this here

Environment Variable:
-D spring.profiles.active=production

System Property:
export spring.profiles.active=production



web.xml init-param doesn’t work for me… But this does…


The above should be added to web.xml (not under a servlet, but just in the root of the file with everything else…) It sets the environment variable and then from there spring will pick up the profile…

Btw, in eclipse, you can configure this by doing the following…

1. Go to the ‘servers’ view.
2. Double click on your server.
3. Click on the ‘launch configuration’ link.
4. Select the ‘environment’ tab.
5. Click ‘new…’
6. Name is: spring.profiles.active
7. Value is: local (or whatever your environment that you want your app to run locally in…)

In your junit tests, simply add @ActiveProfiles(“test”) to the top of the test class.

One hitch:
AWS Beanstalk does not currently allow custom named environment variables to be passed in… That is, you’re stuck with ‘PARAM#’, which is not ‘spring.profiles.active’.

Update - One hitch undone…
AWS Beanstalk does allow java system properties to be passed in. So…


Does in fact work. Note, this will NOT override web.xml, which is a bummer, since could have our default ‘local’ in web.xml and then on our servers override with the proper enviro….






Published by and tagged Code using 336 words.