Skip to content

Commit e72c49f

Browse files
committed
Merge branch cbeams/SPR-9439
* SPR-9439: Introduce ConfigurableWebEnvironment Introduce ConfigurableEnvironment#merge Polish
2 parents 5327a7a + 2a2b6ee commit e72c49f

File tree

15 files changed

+257
-78
lines changed

15 files changed

+257
-78
lines changed

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,19 @@ protected ResourcePatternResolver getResourcePatternResolver() {
378378

379379
/**
380380
* {@inheritDoc}
381-
* <p>The parent {@linkplain #getEnvironment() environment} is
382-
* delegated to this (child) context if the parent is a
383-
* {@link ConfigurableApplicationContext} implementation.
381+
* <p>The parent {@linkplain ApplicationContext#getEnvironment() environment} is
382+
* {@linkplain ConfigurableEnvironment#merge(ConfigurableEnvironment) merged} with
383+
* this (child) application context environment if the parent is non-{@code null} and
384+
* its environment is an instance of {@link ConfigurableEnvironment}.
385+
* @see ConfigurableEnvironment#merge(ConfigurableEnvironment)
384386
*/
385387
public void setParent(ApplicationContext parent) {
386388
this.parent = parent;
387-
if (parent instanceof ConfigurableApplicationContext) {
388-
this.setEnvironment(((ConfigurableApplicationContext)parent).getEnvironment());
389+
if (parent != null) {
390+
Object parentEnvironment = parent.getEnvironment();
391+
if (parentEnvironment instanceof ConfigurableEnvironment) {
392+
this.environment.merge((ConfigurableEnvironment)parentEnvironment);
393+
}
389394
}
390395
}
391396

spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,20 @@ protected String getSystemAttribute(String propertyName) {
387387
return systemProperties;
388388
}
389389

390+
public void merge(ConfigurableEnvironment parent) {
391+
for (PropertySource<?> ps : parent.getPropertySources()) {
392+
if (!this.propertySources.contains(ps.getName())) {
393+
this.propertySources.addLast(ps);
394+
}
395+
}
396+
for (String profile : parent.getActiveProfiles()) {
397+
this.activeProfiles.add(profile);
398+
}
399+
for (String profile : parent.getDefaultProfiles()) {
400+
this.defaultProfiles.add(profile);
401+
}
402+
}
403+
390404

391405
//---------------------------------------------------------------------
392406
// Implementation of ConfigurablePropertyResolver interface

spring-core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,13 +55,14 @@
5555
* propertySources.replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, mockEnvVars);
5656
* </pre>
5757
*
58-
* When an {@link Environment} is being used by an ApplicationContext, it is important
59-
* that any such PropertySource manipulations be performed <em>before</em> the context's
60-
* {@link org.springframework.context.support.AbstractApplicationContext#refresh()
61-
* refresh()} method is called. This ensures that all property sources are available
62-
* during the container bootstrap process, including use by
63-
* {@linkplain org.springframework.context.support.PropertySourcesPlaceholderConfigurer
64-
* property placeholder configurers}.
58+
* When an {@link Environment} is being used by an {@code ApplicationContext}, it is
59+
* important that any such {@code PropertySource} manipulations be performed
60+
* <em>before</em> the context's {@link
61+
* org.springframework.context.support.AbstractApplicationContext#refresh() refresh()}
62+
* method is called. This ensures that all property sources are available during the
63+
* container bootstrap process, including use by {@linkplain
64+
* org.springframework.context.support.PropertySourcesPlaceholderConfigurer property
65+
* placeholder configurers}.
6566
*
6667
*
6768
* @author Chris Beams
@@ -78,7 +79,6 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper
7879
* <p>Any existing active profiles will be replaced with the given arguments; call
7980
* with zero arguments to clear the current set of active profiles. Use
8081
* {@link #addActiveProfile} to add a profile while preserving the existing set.
81-
*
8282
* @see #addActiveProfile
8383
* @see #setDefaultProfiles
8484
* @see org.springframework.context.annotation.Profile
@@ -123,12 +123,10 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper
123123
* Return the value of {@link System#getenv()} if allowed by the current
124124
* {@link SecurityManager}, otherwise return a map implementation that will attempt
125125
* to access individual keys using calls to {@link System#getenv(String)}.
126-
*
127126
* <p>Note that most {@link Environment} implementations will include this system
128127
* environment map as a default {@link PropertySource} to be searched. Therefore, it
129128
* is recommended that this method not be used directly unless bypassing other
130129
* property sources is expressly intended.
131-
*
132130
* <p>Calls to {@link Map#get(Object)} on the Map returned will never throw
133131
* {@link IllegalAccessException}; in cases where the SecurityManager forbids access
134132
* to a property, {@code null} will be returned and an INFO-level log message will be
@@ -140,17 +138,35 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper
140138
* Return the value of {@link System#getProperties()} if allowed by the current
141139
* {@link SecurityManager}, otherwise return a map implementation that will attempt
142140
* to access individual keys using calls to {@link System#getProperty(String)}.
143-
*
144141
* <p>Note that most {@code Environment} implementations will include this system
145142
* properties map as a default {@link PropertySource} to be searched. Therefore, it is
146143
* recommended that this method not be used directly unless bypassing other property
147144
* sources is expressly intended.
148-
*
149145
* <p>Calls to {@link Map#get(Object)} on the Map returned will never throw
150146
* {@link IllegalAccessException}; in cases where the SecurityManager forbids access
151147
* to a property, {@code null} will be returned and an INFO-level log message will be
152148
* issued noting the exception.
153149
*/
154150
Map<String, Object> getSystemProperties();
155151

152+
/**
153+
* Append the given parent environment's active profiles, default profiles and
154+
* property sources to this (child) environment's respective collections of each.
155+
* <p>For any identically-named {@code PropertySource} instance existing in both
156+
* parent and child, the child instance is to be preserved and the parent instance
157+
* discarded. This has the effect of allowing overriding of property sources by the
158+
* child as well as avoiding redundant searches through common property source types,
159+
* e.g. system environment and system properties.
160+
* <p>Active and default profile names are also filtered for duplicates, to avoid
161+
* confusion and redundant storage.
162+
* <p>The parent environment remains unmodified in any case. Note that any changes to
163+
* the parent environment occurring after the call to {@code merge} will not be
164+
* reflected in the child. Therefore, care should be taken to configure parent
165+
* property sources and profile information prior to calling {@code merge}.
166+
* @param parent the environment to merge with
167+
* @since 3.2
168+
* @see org.springframework.context.support.AbstractApplicationContext#setParent
169+
*/
170+
void merge(ConfigurableEnvironment parent);
171+
156172
}

spring-core/src/main/java/org/springframework/util/Assert.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -335,7 +335,7 @@ public static void isInstanceOf(Class type, Object obj, String message) {
335335
notNull(type, "Type to check against must not be null");
336336
if (!type.isInstance(obj)) {
337337
throw new IllegalArgumentException(message +
338-
"Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
338+
". Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
339339
"] must be an instance of " + type);
340340
}
341341
}

spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,22 +16,6 @@
1616

1717
package org.springframework.core.env;
1818

19-
import static java.lang.String.format;
20-
import static org.hamcrest.CoreMatchers.equalTo;
21-
import static org.hamcrest.CoreMatchers.instanceOf;
22-
import static org.hamcrest.CoreMatchers.is;
23-
import static org.hamcrest.CoreMatchers.not;
24-
import static org.hamcrest.CoreMatchers.notNullValue;
25-
import static org.hamcrest.CoreMatchers.nullValue;
26-
import static org.junit.Assert.assertSame;
27-
import static org.junit.Assert.assertThat;
28-
import static org.junit.Assert.fail;
29-
import static org.junit.matchers.JUnitMatchers.hasItem;
30-
import static org.junit.matchers.JUnitMatchers.hasItems;
31-
import static org.springframework.core.env.AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME;
32-
import static org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME;
33-
import static org.springframework.core.env.AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME;
34-
3519
import java.lang.reflect.Field;
3620
import java.security.AccessControlException;
3721
import java.security.Permission;
@@ -40,8 +24,18 @@
4024
import java.util.Map;
4125

4226
import org.junit.Test;
27+
4328
import org.springframework.mock.env.MockPropertySource;
4429

30+
import static java.lang.String.*;
31+
32+
import static org.hamcrest.CoreMatchers.*;
33+
34+
import static org.junit.Assert.*;
35+
import static org.junit.matchers.JUnitMatchers.*;
36+
37+
import static org.springframework.core.env.AbstractEnvironment.*;
38+
4539
/**
4640
* Unit tests for {@link StandardEnvironment}.
4741
*
@@ -62,6 +56,47 @@ public class StandardEnvironmentTests {
6256

6357
private ConfigurableEnvironment environment = new StandardEnvironment();
6458

59+
@Test
60+
public void merge() {
61+
ConfigurableEnvironment child = new StandardEnvironment();
62+
child.setActiveProfiles("c1", "c2");
63+
child.getPropertySources().addLast(
64+
new MockPropertySource("childMock")
65+
.withProperty("childKey", "childVal")
66+
.withProperty("bothKey", "childBothVal"));
67+
68+
ConfigurableEnvironment parent = new StandardEnvironment();
69+
parent.setActiveProfiles("p1", "p2");
70+
parent.getPropertySources().addLast(
71+
new MockPropertySource("parentMock")
72+
.withProperty("parentKey", "parentVal")
73+
.withProperty("bothKey", "parentBothVal"));
74+
75+
assertThat(child.getProperty("childKey"), is("childVal"));
76+
assertThat(child.getProperty("parentKey"), nullValue());
77+
assertThat(child.getProperty("bothKey"), is("childBothVal"));
78+
79+
assertThat(parent.getProperty("childKey"), nullValue());
80+
assertThat(parent.getProperty("parentKey"), is("parentVal"));
81+
assertThat(parent.getProperty("bothKey"), is("parentBothVal"));
82+
83+
assertThat(child.getActiveProfiles(), equalTo(new String[]{"c1","c2"}));
84+
assertThat(parent.getActiveProfiles(), equalTo(new String[]{"p1","p2"}));
85+
86+
child.merge(parent);
87+
88+
assertThat(child.getProperty("childKey"), is("childVal"));
89+
assertThat(child.getProperty("parentKey"), is("parentVal"));
90+
assertThat(child.getProperty("bothKey"), is("childBothVal"));
91+
92+
assertThat(parent.getProperty("childKey"), nullValue());
93+
assertThat(parent.getProperty("parentKey"), is("parentVal"));
94+
assertThat(parent.getProperty("bothKey"), is("parentBothVal"));
95+
96+
assertThat(child.getActiveProfiles(), equalTo(new String[]{"c1","c2","p1","p2"}));
97+
assertThat(parent.getActiveProfiles(), equalTo(new String[]{"p1","p2"}));
98+
}
99+
65100
@Test
66101
public void propertySourceOrder() {
67102
ConfigurableEnvironment env = new StandardEnvironment();

spring-web/src/main/java/org/springframework/web/context/ConfigurableWebApplicationContext.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,6 +71,11 @@ public interface ConfigurableWebApplicationContext extends WebApplicationContext
7171
*/
7272
ServletConfig getServletConfig();
7373

74+
/**
75+
* Return the {@link ConfigurableWebEnvironment} used by this web application context.
76+
*/
77+
ConfigurableWebEnvironment getEnvironment();
78+
7479
/**
7580
* Set the namespace for this web application context,
7681
* to be used for building a default context config location.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.context;
18+
19+
import javax.servlet.ServletConfig;
20+
import javax.servlet.ServletContext;
21+
22+
import org.springframework.core.env.ConfigurableEnvironment;
23+
24+
/**
25+
* Specialization of {@link ConfigurableEnvironment} allowing initialization of
26+
* servlet-related {@link org.springframework.core.env.PropertySource} objects at the
27+
* earliest moment the {@link ServletContext} and (optionally) {@link ServletConfig}
28+
* become available.
29+
*
30+
* @author Chris Beams
31+
* @since 3.1.2
32+
* @see ConfigurableWebApplicationContext#getEnvironment()
33+
*/
34+
public interface ConfigurableWebEnvironment extends ConfigurableEnvironment {
35+
36+
/**
37+
* Replace any {@linkplain
38+
* org.springframework.core.env.PropertySource.StubPropertySource stub property source}
39+
* instances acting as placeholders with real servlet context/config property sources
40+
* using the given parameters.
41+
* @param servletContext the {@link ServletContext} (may not be {@code null})
42+
* @param servletConfig the {@link ServletContext} ({@code null} if not available)
43+
*/
44+
void initPropertySources(ServletContext servletContext, ServletConfig servletConfig);
45+
46+
}

spring-web/src/main/java/org/springframework/web/context/ContextLoader.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.springframework.util.ClassUtils;
4545
import org.springframework.util.ObjectUtils;
4646
import org.springframework.util.StringUtils;
47-
import org.springframework.web.context.support.WebApplicationContextUtils;
4847

4948
/**
5049
* Performs the actual initialization work for the root application context.
@@ -485,15 +484,9 @@ protected void customizeContext(ServletContext servletContext, ConfigurableWebAp
485484
initializerInstances.add(BeanUtils.instantiateClass(initializerClass));
486485
}
487486

488-
Collections.sort(initializerInstances, new AnnotationAwareOrderComparator());
489-
490-
// eagerly attempt to initialize servlet property sources in case initializers
491-
// below depend on accessing context-params via the Environment API. Note that
492-
// depending on application context implementation, this initialization will be
493-
// attempted again during context refresh.
494-
WebApplicationContextUtils.initServletPropertySources(
495-
applicationContext.getEnvironment().getPropertySources(), servletContext);
487+
applicationContext.getEnvironment().initPropertySources(servletContext, null);
496488

489+
Collections.sort(initializerInstances, new AnnotationAwareOrderComparator());
497490
for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : initializerInstances) {
498491
initializer.initialize(applicationContext);
499492
}

spring-web/src/main/java/org/springframework/web/context/support/AbstractRefreshableWebApplicationContext.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,9 @@
2727
import org.springframework.ui.context.Theme;
2828
import org.springframework.ui.context.ThemeSource;
2929
import org.springframework.ui.context.support.UiApplicationContextUtils;
30+
import org.springframework.util.Assert;
3031
import org.springframework.web.context.ConfigurableWebApplicationContext;
32+
import org.springframework.web.context.ConfigurableWebEnvironment;
3133
import org.springframework.web.context.ServletConfigAware;
3234
import org.springframework.web.context.ServletContextAware;
3335

@@ -139,6 +141,14 @@ protected ConfigurableEnvironment createEnvironment() {
139141
return new StandardServletEnvironment();
140142
}
141143

144+
@Override
145+
public ConfigurableWebEnvironment getEnvironment() {
146+
ConfigurableEnvironment env = super.getEnvironment();
147+
Assert.isInstanceOf(ConfigurableWebEnvironment.class, env,
148+
"ConfigurableWebApplicationContext environment must be of type " +
149+
"ConfigurableWebEnvironment");
150+
return (ConfigurableWebEnvironment) env;
151+
}
142152

143153
/**
144154
* Register request/session scopes, a {@link ServletContextAwareProcessor}, etc.
@@ -186,9 +196,7 @@ protected void onRefresh() {
186196
@Override
187197
protected void initPropertySources() {
188198
super.initPropertySources();
189-
WebApplicationContextUtils.initServletPropertySources(
190-
this.getEnvironment().getPropertySources(), this.servletContext,
191-
this.servletConfig);
199+
this.getEnvironment().initPropertySources(this.servletContext, this.servletConfig);
192200
}
193201

194202
public Theme getTheme(String themeName) {

0 commit comments

Comments
 (0)