Introduce ConfigurableEnvironment#merge
Prior to this change, AbstractApplicationContext#setParent replaced the
child context's Environment with the parent's Environment if available.
This has the negative effect of potentially changing the type of the
child context's Environment, and in any case causes property sources
added directly against the child environment to be ignored. This
situation could easily occur if a WebApplicationContext child had a
non-web ApplicationContext set as its parent. In this case the parent
Environment type would (likely) be StandardEnvironment, while the child
Environment type would (likely) be StandardServletEnvironment. By
directly inheriting the parent environment, critical property sources
such as ServletContextPropertySource are lost entirely.
This commit introduces the concept of merging an environment through
the new ConfigurableEnvironment#merge method. Instead of replacing the
child's environment with the parent's,
AbstractApplicationContext#setParent now merges property sources as
well as active and default profile names from the parent into the
child. In this way, distinct environment objects are maintained with
specific types and property sources preserved. See #merge Javadoc for
additional details.
Issue: SPR-9446
Backport-Issue: SPR-9444, SPR-9439
Backport-Commit: 9fcfd7e827
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@@ -16,9 +16,6 @@
|
||||
|
||||
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;
|
||||
@@ -36,6 +33,10 @@ import org.springframework.context.TestListener;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.XmlWebApplicationContext;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
@@ -47,12 +48,14 @@ public class XmlWebApplicationContextTests extends AbstractApplicationContextTes
|
||||
protected ConfigurableApplicationContext createContext() throws Exception {
|
||||
InitAndIB.constructed = false;
|
||||
root = new XmlWebApplicationContext();
|
||||
root.getEnvironment().addActiveProfile("rootProfile1");
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
root.setServletContext(sc);
|
||||
root.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/applicationContext.xml"});
|
||||
root.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
|
||||
beanFactory.addBeanPostProcessor(new BeanPostProcessor() {
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
|
||||
if (bean instanceof TestBean) {
|
||||
((TestBean) bean).getFriends().add("myFriend");
|
||||
@@ -67,6 +70,7 @@ public class XmlWebApplicationContextTests extends AbstractApplicationContextTes
|
||||
});
|
||||
root.refresh();
|
||||
XmlWebApplicationContext wac = new XmlWebApplicationContext();
|
||||
wac.getEnvironment().addActiveProfile("wacProfile1");
|
||||
wac.setParent(root);
|
||||
wac.setServletContext(sc);
|
||||
wac.setNamespace("test-servlet");
|
||||
@@ -75,8 +79,11 @@ public class XmlWebApplicationContextTests extends AbstractApplicationContextTes
|
||||
return wac;
|
||||
}
|
||||
|
||||
public void testEnvironmentInheritance() {
|
||||
assertThat(this.applicationContext.getEnvironment(), sameInstance(this.root.getEnvironment()));
|
||||
public void testEnvironmentMerge() {
|
||||
assertThat(this.root.getEnvironment().acceptsProfiles("rootProfile1"), is(true));
|
||||
assertThat(this.root.getEnvironment().acceptsProfiles("wacProfile1"), is(false));
|
||||
assertThat(this.applicationContext.getEnvironment().acceptsProfiles("rootProfile1"), is(true));
|
||||
assertThat(this.applicationContext.getEnvironment().acceptsProfiles("wacProfile1"), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user