Announcement Announcement Module
Collapse
No announcement yet.
Bean Profiles - Not activating via web.xml but with contextInitializerClasses Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bean Profiles - Not activating via web.xml but with contextInitializerClasses

    Previously, I posted the following: http://forum.springsource.org/showth...RC1-An-example

    For some reason, the "web-dev" profile is not getting activated when I'm running my app in Tomcat. How do I know? It keeps telling me that I'm missing beans (that I've declared under the "web-dev" profile).

    Based on http://blog.springsource.com/2011/02...1-m1-released/, to activate via web.xml, you declare:

    Code:
    <servlet>
    	<servlet-name>spring</servlet-name>
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	<init-param>
    	<param-name>spring.profiles.active</param-name>
    	<param-value>PROFILE HERE</param-value>
    	</init-param>
    </servlet>
    But it doesn't work. To make it work, I had to follow the suggestion from this blog http://raymondhlee.wordpress.com/201...t-environment/ and from this JIRA https://issues.springsource.org/browse/SPR-8366

    Basically I had to declare under web.xml:

    Code:
    	<context-param>
    		<param-name>contextInitializerClasses</param-name>
    		<param-value>org.myapp.here.ContextProfileInitializer</param-value>
    	</context-param>
    Code:
    package org.myapp.here;
    
    import org.springframework.context.ApplicationContextInitializer;
    import org.springframework.core.env.ConfigurableEnvironment;
    import org.springframework.web.context.ConfigurableWebApplicationContext;
    
    public class ContextProfileInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> {
    	
    	public void initialize(ConfigurableWebApplicationContext ctx) {
    		ConfigurableEnvironment environment = ctx.getEnvironment();
    		environment.setActiveProfiles("web-dev");
    	}
    	
    }


    Here's my applicationContext.xml fragment:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:p="http://www.springframework.org/schema/p" 
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	   		http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    	   		http://www.springframework.org/schema/context
    	   		http://www.springframework.org/schema/context/spring-context-3.1.xsd
    			http://www.springframework.org/schema/mvc 
    			http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    			
    	<context:property-placeholder properties-ref="deployProperties" />
     
     	<!-- Import bean definitions -->
    	<beans profile="web-dev, test-dev">
    		<import resource="trace-context.xml"/>
    		<import resource="spring-data-jpa.xml"/>
    		<import resource="spring-security-roles.xml" />
    	</beans>
    	
    	<beans profile="web-dev">
    		<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"
    				p:location="/WEB-INF/spring.properties" />
            
    		<import resource="spring-cache.xml"/>
    		<import resource="tiles-context.xml" />
    		<import resource="themes-context.xml" />
    	</beans>  
    	
    	<beans profile="test-dev">
    		<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"
    				p:location="classpath:spring.properties" />
    	</beans>
    </beans>

  • #2
    I think it work with context-param

    Seems this also works but i am already in my firs testing stage, so I am not 100% sure:

    <context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>webflow-development,security-simple</param-value>
    </context-param>

    Comment

    Working...
    X