Announcement Announcement Module
Collapse
No announcement yet.
ApplicationContextInitializer w/ Parent Context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ApplicationContextInitializer w/ Parent Context

    With a parent context specified, it seems that MyInitializer (implements ApplicationContextInitializer) doesn't get executed until after the parent context has loaded. This is problematic, since I want to set the active Spring profile in MyInitializer prior to the parent context being loaded. Otherwise, any beans w/ a profile don't get loaded.

    Is this a bug?

    web.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
    	xmlns="http://java.sun.com/xml/ns/javaee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    	<context-param>
    		<param-name>parentContextKey</param-name>
    		<param-value>earContext</param-value>
    	</context-param>
    
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    		/WEB-INF/classes/root-context.xml
    		</param-value>
    	</context-param>
    
    	<context-param>
    		<param-name>contextInitializerClasses</param-name>
    		<param-value>com.test.util.MyInitializer</param-value>
    	</context-param>
    
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    
    	<servlet>
    		<servlet-name>MyApp</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/classes/myapp-context.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>myApp</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    
    </web-app>

    My ApplicationContextInitializer
    Code:
    package com.test.util;
    
    import org.springframework.context.ApplicationContextInitializer;
    import org.springframework.core.env.ConfigurableEnvironment;
    import org.springframework.web.context.ConfigurableWebApplicationContext;
    
    public class MyInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> {
    
    	public void initialize(ConfigurableWebApplicationContext ctx) {
    		System.out.println("MADEIT");
    		
    		ConfigurableEnvironment environment = ctx.getEnvironment();
    		environment.setActiveProfiles("development");
    	}
    
    }

  • #2
    I'm facing the same problem, any suggestions?

    Thanks

    Comment


    • #3
      The problem is you're loading a parent context which doesn't use the environment you've specified. I tried adding a property source for the parent context which contained spring.profiles.active but this was ignored as well (using both XML config and @Configuration static bean). It seems as though you need to get access to the creation of the Environment but nothing I tried worked.

      My solution isn't great but I just did a System.setProperty("spring.profiles.active", profile) in the ApplicationContextInitializer. This then got picked up by the parent context. Your context initialiser should then load your parent context. You;ll also need to change your web.xml to NOT load the parent context via the ContextLoaderListener so you should not have locatorFactorySelector amd parentContextKey specified. To load your parent context in your contextinitialiser, you this code:

      Code:
      BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance("classpath*:beanRefContext.xml");
      BeanFactoryReference parentContextRef = locator.useBeanFactory("ear.context");        
      ConfigurableApplicationContext parentContext = (ConfigurableApplicationContext) parentContextRef.getFactory();        
      applicationContext.setParent(parentContext);
      My contextInitialiser is passed a brand new applicationContext with no beans declared on it (other than a property source with the profile I want). I use the contextInitiaser to decorate/register it with the appropriate config beans, etc. I do this after I setup the parent.
      Last edited by lukepop; Mar 18th, 2013, 08:27 PM.

      Comment

      Working...
      X