Initialize spring bean profile through ContextLoaderListener in web.xml

bachr picture bachr · Feb 5, 2014 · Viewed 10.9k times · Source

In my web.xml I'm declaring a ContextLoaderListener to configure spring application this way:

<listener>    
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
</listener>

In one of my spring configuration xml files, I'm using different beans profile for development and production.

<beans profile="production">
   <bean />
</beans
<beans profile="development">
   <bean />
</beans

How I could set the default beans profile in the web.xml? is there something similar to the following when using ContextLoaderListener instead of spring servlet:

<init-param>
   <param-name>spring.profiles.active</param-name>
   <param-value>production</param-value>
</init-param>

Answer

nobeh picture nobeh · Feb 5, 2014

You can configure web.xml at the level of ContextLoaderListener with:

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>profileName</param-value>
</context-param>

and the level of DispatcherServlet with:

<init-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>profileName</param-value>
</init-param>

Reference: http://spring.io/blog/2011/02/11/spring-framework-3-1-m1-released/