Cette page n'est pas disponible en français. Veuillez-nous en excuser.

ABIS Infor - 2014-03

Spring: Bean Definition Profiles

Sandy Schillebeeckx (ABIS) - 26 february 2014

Abstract

Spring 3.1 introduced so-called Bean Definition Profiles that allow for registration of different beans in different environments. One of the most common cases would be working against a standalone datasource in development phase vs looking up that same datasource from JNDI when in production. Bean definition profiles represent a general-purpose way to satisfy use cases of this kind.

Goal

In any (larger) project you'll have to think about how to solve the environment specific configuration of the application. Developers tend to run an application in a lightweight servlet container like Tomcat with a local open source database for persistence. In production you want to run your application in a more heavy weight application server like WebSphere.

Perhaps one of the most common cases would be working against a standalone datasource in development vs looking up that same datasource from JNDI in production.

Before, you would solve this by using different subdirectories named after your environment, i.e. development, production etc. Then you would write some intelligent bean loader mechanism that dynamically includes the correct configuration files. Which worked of course, but it was quite a hassle.

Spring 3.1 addressed this problem and came up with a solution with the so called bean definition profiles.These allow you to do 2 things: you can specify a profile attribute in a beans element and you are allowed to nest beans-elements, so you can have all profiles in one XML file.

Use

Configuration of profiles (XML)

Implementing this new profile attribute is very easy. In your applicationContext.xml file, you add them like this:

<beans>
<beans profile="development">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://myHost:portNumber/mySchema"/>
<property name="username" value="myUserId"/>
<property name="password" value="myPassword"/>
</bean>
</beans>

<beans profile="production">
<jee:jndi-lookup id="dataSource jndi-name="java:comp/env/jdbc/myDSname"/>
</beans>
</beans>

Activating profiles

In order to set the correct environment, you will have to activate one of your profiles. This can be done in several ways. The implementations are based on the newly introduced classes Environment and PropertySource that mainly do there work behind the scenes.

Programmatically

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles("development");

In this way, you still need to get into the code to be able to change the profile and redeploy the whole thing. The next two methods increase the flexibility of switching the profile from "the outside".

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>development</param-value>

</init-param>
</servlet>

Declaratively (JVM parameter)

-Dspring.profiles.active="development"

Conclusion

Bean Definition Profiles (introduced in Spring 3.1) provide a means to switch environments for your applications. It is one of the many things that Spring provides to help you configure your applications.

Things didn't stop there of course. In the meanwhile, Spring 4.0 was released in December 2013. It promises to fully integrate all of the new features of Java SE 8 (like lambda expressions) when it will be finally released. And much more...

If you want to learn more on how to use Spring in your applications, join us in one of the next courses. For more information, please visit http://www.abis.be/html/en1369.html.