Explicitly copy over properties from bootstrap.properties
Fixes gh-273
This commit is contained in:
@@ -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<String, Object> targetMap = ((MapPropertySource) target).getSource();
|
||||
if (target == source) {
|
||||
return;
|
||||
}
|
||||
if (source instanceof MapPropertySource) {
|
||||
Map<String, Object> 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<String, Object> targetMap = ((MapPropertySource) target)
|
||||
.getSource();
|
||||
if (target != source) {
|
||||
if (source instanceof MapPropertySource) {
|
||||
Map<String, Object> 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,
|
||||
|
||||
@@ -194,8 +194,8 @@ public class BootstrapConfigurationTests {
|
||||
ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
environment.getPropertySources().addLast(new MapPropertySource("last",
|
||||
Collections.<String, Object>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"));
|
||||
|
||||
Reference in New Issue
Block a user