| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5874383 commit 9fcfd7e
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -378,14 +378,19 @@ protected ResourcePatternResolver getResourcePatternResolver() { | |||
| 378 | 378 | ||
| 379 | 379 | /** | |
| 380 | 380 | * {@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) | ||
| 384 | 386 | */ | |
| 385 | 387 | public void setParent(ApplicationContext parent) { | |
| 386 | 388 | 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 | + } | ||
| 389 | 394 | } | |
| 390 | 395 | } | |
| 391 | 396 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -387,6 +387,20 @@ protected String getSystemAttribute(String propertyName) { | |||
| 387 | 387 | return systemProperties; | |
| 388 | 388 | } | |
| 389 | 389 | ||
| 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 | + | ||
| 390 | 404 | ||
| 391 | 405 | //--------------------------------------------------------------------- | |
| 392 | 406 | // Implementation of ConfigurablePropertyResolver interface | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -149,4 +149,24 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper | |||
| 149 | 149 | */ | |
| 150 | 150 | Map<String, Object> getSystemProperties(); | |
| 151 | 151 | ||
| 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 | + | ||
| 152 | 172 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,6 +56,47 @@ public class StandardEnvironmentTests { | |||
| 56 | 56 | ||
| 57 | 57 | private ConfigurableEnvironment environment = new StandardEnvironment(); | |
| 58 | 58 | ||
| 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 | + | ||
| 59 | 100 | @Test | |
| 60 | 101 | public void propertySourceOrder() { | |
| 61 | 102 | ConfigurableEnvironment env = new StandardEnvironment(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,6 +48,7 @@ public class XmlWebApplicationContextTests extends AbstractApplicationContextTes | |||
| 48 | 48 | protected ConfigurableApplicationContext createContext() throws Exception { | |
| 49 | 49 | InitAndIB.constructed = false; | |
| 50 | 50 | root = new XmlWebApplicationContext(); | |
| 51 | + root.getEnvironment().addActiveProfile("rootProfile1"); | ||
| 51 | 52 | MockServletContext sc = new MockServletContext(""); | |
| 52 | 53 | root.setServletContext(sc); | |
| 53 | 54 | root.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/applicationContext.xml"}); | |
@@ -69,6 +70,7 @@ public Object postProcessAfterInitialization(Object bean, String name) throws Be | |||
| 69 | 70 | }); | |
| 70 | 71 | root.refresh(); | |
| 71 | 72 | XmlWebApplicationContext wac = new XmlWebApplicationContext(); | |
| 73 | + wac.getEnvironment().addActiveProfile("wacProfile1"); | ||
| 72 | 74 | wac.setParent(root); | |
| 73 | 75 | wac.setServletContext(sc); | |
| 74 | 76 | wac.setNamespace("test-servlet"); | |
@@ -77,8 +79,11 @@ public Object postProcessAfterInitialization(Object bean, String name) throws Be | |||
| 77 | 79 | return wac; | |
| 78 | 80 | } | |
| 79 | 81 | ||
| 80 | - public void testEnvironmentInheritance() { | ||
| 81 | - assertThat(this.applicationContext.getEnvironment(), sameInstance(this.root.getEnvironment())); | ||
| 82 | + public void testEnvironmentMerge() { | ||
| 83 | + assertThat(this.root.getEnvironment().acceptsProfiles("rootProfile1"), is(true)); | ||
| 84 | + assertThat(this.root.getEnvironment().acceptsProfiles("wacProfile1"), is(false)); | ||
| 85 | + assertThat(this.applicationContext.getEnvironment().acceptsProfiles("rootProfile1"), is(true)); | ||
| 86 | + assertThat(this.applicationContext.getEnvironment().acceptsProfiles("wacProfile1"), is(true)); | ||
| 82 | 87 | } | |
| 83 | 88 | ||
| 84 | 89 | /** | |
| Back | FazBrowse Home | New Git URL |
0 commit comments