diff --git a/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java b/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java index ecc13c9a52..84831884b7 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java +++ b/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java @@ -376,8 +376,17 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader // Implementation of ConfigurableApplicationContext interface //--------------------------------------------------------------------- + /** + * {@inheritDoc} + *

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) { diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/AbstractRefreshablePortletApplicationContext.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/AbstractRefreshablePortletApplicationContext.java index 4269eed798..459a03c764 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/AbstractRefreshablePortletApplicationContext.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/AbstractRefreshablePortletApplicationContext.java @@ -87,6 +87,15 @@ public abstract class AbstractRefreshablePortletApplicationContext extends Abstr setDisplayName("Root PortletApplicationContext"); } + /** + * {@inheritDoc} + *

The parent {@linkplain #getEnvironment() environment} is + * delegated to this (child) context if the parent is a + * {@link ConfigurableApplicationContext} implementation. + *

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); diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/StaticPortletApplicationContext.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/StaticPortletApplicationContext.java index 56011eb7cd..7ffc763365 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/StaticPortletApplicationContext.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/StaticPortletApplicationContext.java @@ -84,6 +84,15 @@ public class StaticPortletApplicationContext extends StaticApplicationContext this.portletContext, this.portletConfig); } + /** + * {@inheritDoc} + *

The parent {@linkplain #getEnvironment() environment} is + * delegated to this (child) context if the parent is a + * {@link ConfigurableApplicationContext} implementation. + *

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); diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/context/XmlWebApplicationContextTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/context/XmlWebApplicationContextTests.java index 91117a7956..b20b13220b 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/context/XmlWebApplicationContextTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/context/XmlWebApplicationContextTests.java @@ -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()