Skip to content

Commit

Permalink
Explicitly copy over properties from bootstrap.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Syer committed Nov 14, 2017
1 parent 7eb3100 commit a513d0a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,25 @@ private Collection<? extends ApplicationListener<?>> filterListeners(
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));
}
}
}
}
}
Expand All @@ -252,7 +252,7 @@ private void mergeAdditionalPropertySources(MutablePropertySources environment,
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())) {
Expand All @@ -262,8 +262,18 @@ private void mergeAdditionalPropertySources(MutablePropertySources environment,
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ public void overrideAllWhenOverrideAllowed() {
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"));
}

Expand Down Expand Up @@ -288,9 +288,11 @@ public void bootstrapContextSharedBySiblings() {
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());
Expand Down Expand Up @@ -326,7 +328,8 @@ public void differentProfileInChild() {
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(),
Expand Down Expand Up @@ -354,15 +357,15 @@ public void differentProfileInChild() {
@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"));
Expand Down

0 comments on commit a513d0a

Please sign in to comment.