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.
http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/
Profiles allow you to add / remove features / settings of your application based on the environment it is actually running in.
Example:
<beans profile=“beta”>
<bean id=“appProperties” class=“wookets.bootstrap.AppProperties”>
<property name=“someprop” value=“CHEESE”/>
</bean>
</beans>
<beans profile=“local”>
<bean id=“appProperties” class=“wookets.bootstrap.AppProperties”>
<property name=“someprop” value=“WAFFLES”/>
</bean>
</beans>
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:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
</servlet>
Update:
web.xml init-param doesn’t work for me… But this does…
<env-entry>
<env-entry-name>spring.profiles.active</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>local</env-entry-value>
</env-entry>
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…
-Dspring.profiles.active=beta
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….
Resources:
http://javasplitter.blogspot.com/2011/06/bean-definition-profiles-and.html
http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/
https://www.google.com/search?gcx=c&sourceid=chrome&ie=UTF-8&q=aws+beanstalk+custom+environment+variables
http://blog.springsource.com/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles/