diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java index 10a2dbd0..c292ffa8 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java @@ -220,25 +220,25 @@ public class BootstrapApplicationListener private void mergeDefaultProperties(MutablePropertySources environment, MutablePropertySources bootstrap) { String name = DEFAULT_PROPERTIES; - if (!bootstrap.contains(name)) { - return; - } - PropertySource source = bootstrap.get(name); - if (!environment.contains(name)) { - environment.addLast(source); - } - else { - PropertySource target = environment.get(name); - if (target instanceof MapPropertySource) { - Map targetMap = ((MapPropertySource) target).getSource(); - if (target == source) { - return; - } - if (source instanceof MapPropertySource) { - Map map = ((MapPropertySource) source).getSource(); - for (String key : map.keySet()) { - if (!target.containsProperty(key)) { - targetMap.put(key, map.get(key)); + if (bootstrap.contains(name)) { + PropertySource source = bootstrap.get(name); + if (!environment.contains(name)) { + environment.addLast(source); + } + else { + PropertySource target = environment.get(name); + if (target instanceof MapPropertySource) { + Map targetMap = ((MapPropertySource) target) + .getSource(); + if (target != source) { + if (source instanceof MapPropertySource) { + Map map = ((MapPropertySource) source) + .getSource(); + for (String key : map.keySet()) { + if (!target.containsProperty(key)) { + targetMap.put(key, map.get(key)); + } + } } } } @@ -252,7 +252,7 @@ public class BootstrapApplicationListener PropertySource defaultProperties = environment.get(DEFAULT_PROPERTIES); ExtendedDefaultPropertySource result = defaultProperties instanceof ExtendedDefaultPropertySource ? (ExtendedDefaultPropertySource) defaultProperties - : new ExtendedDefaultPropertySource(defaultProperties.getName(), + : new ExtendedDefaultPropertySource(DEFAULT_PROPERTIES, defaultProperties); for (PropertySource source : bootstrap) { if (!environment.contains(source.getName())) { @@ -262,8 +262,18 @@ public class BootstrapApplicationListener for (String name : result.getPropertySourceNames()) { bootstrap.remove(name); } - environment.replace(DEFAULT_PROPERTIES, result); - bootstrap.replace(DEFAULT_PROPERTIES, result); + addOrReplace(environment, result); + addOrReplace(bootstrap, result); + } + + private void addOrReplace(MutablePropertySources environment, + PropertySource result) { + if (environment.contains(result.getName())) { + environment.replace(result.getName(), result); + } + else { + environment.addLast(result); + } } private void addAncestorInitializer(SpringApplication application, diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java index 2d6092f9..2cec0457 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java @@ -194,8 +194,8 @@ public class BootstrapConfigurationTests { ConfigurableEnvironment environment = new StandardEnvironment(); environment.getPropertySources().addLast(new MapPropertySource("last", Collections.singletonMap("bootstrap.foo", "splat"))); - this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE).environment(environment) - .sources(BareConfiguration.class).run(); + this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE) + .environment(environment).sources(BareConfiguration.class).run(); assertEquals("splat", this.context.getEnvironment().getProperty("bootstrap.foo")); } @@ -288,9 +288,11 @@ public class BootstrapConfigurationTests { SpringApplicationBuilder builder = new SpringApplicationBuilder() .sources(BareConfiguration.class); this.sibling = builder.child(BareConfiguration.class) - .properties("spring.application.name=sibling").web(WebApplicationType.NONE).run(); + .properties("spring.application.name=sibling") + .web(WebApplicationType.NONE).run(); this.context = builder.child(BareConfiguration.class) - .properties("spring.application.name=context").web(WebApplicationType.NONE).run(); + .properties("spring.application.name=context") + .web(WebApplicationType.NONE).run(); assertEquals(1, TestHigherPriorityBootstrapConfiguration.count.get()); assertNotNull(context.getParent()); assertEquals("bootstrap", context.getParent().getParent().getId()); @@ -326,7 +328,8 @@ public class BootstrapConfigurationTests { PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar"); // Profiles are always merged with the child ConfigurableApplicationContext parent = new SpringApplicationBuilder() - .sources(BareConfiguration.class).profiles("parent").web(WebApplicationType.NONE).run(); + .sources(BareConfiguration.class).profiles("parent") + .web(WebApplicationType.NONE).run(); this.context = new SpringApplicationBuilder(BareConfiguration.class) .profiles("child").parent(parent).web(WebApplicationType.NONE).run(); assertNotSame(this.context.getEnvironment(), @@ -354,15 +357,15 @@ public class BootstrapConfigurationTests { @Test public void includeProfileFromBootstrapPropertySource() { PropertySourceConfiguration.MAP.put("spring.profiles.include", "bar,baz"); - this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE).profiles("foo") - .sources(BareConfiguration.class).run(); + this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE) + .profiles("foo").sources(BareConfiguration.class).run(); assertTrue(this.context.getEnvironment().acceptsProfiles("baz")); assertTrue(this.context.getEnvironment().acceptsProfiles("bar")); } @Test public void includeProfileFromBootstrapProperties() { - this.context = new SpringApplicationBuilder().web(false) + this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE) .sources(BareConfiguration.class) .properties("spring.cloud.bootstrap.name=local").run(); assertTrue(this.context.getEnvironment().acceptsProfiles("local"));