Delegate parent environment to child app contexts
Calls to AbstractApplicationContext#setParent delegate the parent context environment to the child. This ensures that any property sources added to the parent are available to the child as well as ensuring that any profiles activated are activated everywhere. Child contexts may still choose to replace their environment (through an ApplicationContextInitializer, for example). In any case, however, in the root/child web application context relationship established by ContextLoader + DispatcherServlet, the child is guaranteed to have already been given the parent environment by the time it is delegated to any ACIs. See AbstractApplicationContext#setParent for implementation See FrameworkServlet#createWebApplicationContext for order in which setParent then initializeWebApplicationContext are called. Issue: SPR-8185
This commit is contained in:
@@ -376,8 +376,17 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
||||
// Implementation of ConfigurableApplicationContext interface
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>The parent {@linkplain #getEnvironment() environment} is
|
||||
* delegated to this (child) context if the parent is a
|
||||
* {@link ConfigurableApplicationContext} implementation.
|
||||
*/
|
||||
public void setParent(ApplicationContext parent) {
|
||||
this.parent = parent;
|
||||
if (parent instanceof ConfigurableApplicationContext) {
|
||||
this.setEnvironment(((ConfigurableApplicationContext)parent).getEnvironment());
|
||||
}
|
||||
}
|
||||
|
||||
public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor) {
|
||||
|
||||
@@ -87,6 +87,15 @@ public abstract class AbstractRefreshablePortletApplicationContext extends Abstr
|
||||
setDisplayName("Root PortletApplicationContext");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>The parent {@linkplain #getEnvironment() environment} is
|
||||
* delegated to this (child) context if the parent is a
|
||||
* {@link ConfigurableApplicationContext} implementation.
|
||||
* <p>The parent {@linkplain #getServletContext() servlet context} is
|
||||
* delegated to this (child) context if the parent is a {@link WebApplicationContext}
|
||||
* implementation.
|
||||
*/
|
||||
@Override
|
||||
public void setParent(ApplicationContext parent) {
|
||||
super.setParent(parent);
|
||||
|
||||
@@ -84,6 +84,15 @@ public class StaticPortletApplicationContext extends StaticApplicationContext
|
||||
this.portletContext, this.portletConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>The parent {@linkplain #getEnvironment() environment} is
|
||||
* delegated to this (child) context if the parent is a
|
||||
* {@link ConfigurableApplicationContext} implementation.
|
||||
* <p>The parent {@linkplain #getServletContext() servlet context} is
|
||||
* delegated to this (child) context if the parent is a {@link WebApplicationContext}
|
||||
* implementation.
|
||||
*/
|
||||
@Override
|
||||
public void setParent(ApplicationContext parent) {
|
||||
super.setParent(parent);
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.web.context;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
@@ -72,6 +75,10 @@ public class XmlWebApplicationContextTests extends AbstractApplicationContextTes
|
||||
return wac;
|
||||
}
|
||||
|
||||
public void testEnvironmentInheritance() {
|
||||
assertThat(this.applicationContext.getEnvironment(), sameInstance(this.root.getEnvironment()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden as we can't trust superclass method
|
||||
* @see org.springframework.context.AbstractApplicationContextTests#testEvents()
|
||||
|
||||
Reference in New Issue
Block a user