Browse Source

Simplify logic with boot strap property sources a bit

We only need one special name for a property source. In the end it
gets used twice, but this simplifies the logic a bit and ensures that
there is never a PropertySource with that name and things like
spring.application.name=bootstrap in it.
pull/84/head
Dave Syer 9 years ago
parent
commit
532cb8efc7
  1. 19
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java
  2. 82
      spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java

19
spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java

@ -17,7 +17,6 @@ @@ -17,7 +17,6 @@
package org.springframework.cloud.bootstrap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -72,7 +71,7 @@ public class BootstrapApplicationListener @@ -72,7 +71,7 @@ public class BootstrapApplicationListener
return;
}
// don't listen to events in a bootstrap context
if (environment.getPropertySources().contains("bootstrapInProgress")) {
if (environment.getPropertySources().contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
return;
}
ConfigurableApplicationContext context = bootstrapServiceContext(environment,
@ -99,8 +98,6 @@ public class BootstrapApplicationListener @@ -99,8 +98,6 @@ public class BootstrapApplicationListener
}
bootstrapProperties.addFirst(
new MapPropertySource(BOOTSTRAP_PROPERTY_SOURCE_NAME, bootstrapMap));
bootstrapProperties.addFirst(new MapPropertySource("bootstrapInProgress",
Collections.<String, Object> emptyMap()));
for (PropertySource<?> source : environment.getPropertySources()) {
bootstrapProperties.addLast(source);
}
@ -129,7 +126,9 @@ public class BootstrapApplicationListener @@ -129,7 +126,9 @@ public class BootstrapApplicationListener
final ConfigurableApplicationContext context = builder.run();
// Make the bootstrap context a parent of the app context
addAncestorInitializer(application, context);
bootstrapProperties.remove("bootstrapInProgress");
// It only has properties in it now that we don't want in the parent so remove
// it (and it will be added back later)
bootstrapProperties.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME);
return context;
}
@ -219,9 +218,6 @@ public class BootstrapApplicationListener @@ -219,9 +218,6 @@ public class BootstrapApplicationListener
@Override
public void initialize(ConfigurableApplicationContext context) {
preemptMerge(context.getEnvironment().getPropertySources(),
new MapPropertySource(BOOTSTRAP_PROPERTY_SOURCE_NAME,
Collections.<String, Object> emptyMap()));
while (context.getParent() != null && context.getParent() != context) {
context = (ConfigurableApplicationContext) context.getParent();
}
@ -229,13 +225,6 @@ public class BootstrapApplicationListener @@ -229,13 +225,6 @@ public class BootstrapApplicationListener
.initialize(context);
}
private void preemptMerge(MutablePropertySources propertySources,
PropertySource<?> propertySource) {
if (propertySource != null
&& !propertySources.contains(propertySource.getName())) {
propertySources.addFirst(propertySource);
}
}
}
/**

82
spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java

@ -75,10 +75,10 @@ public class BootstrapConfigurationTests { @@ -75,10 +75,10 @@ public class BootstrapConfigurationTests {
.sources(BareConfiguration.class)
.properties("spring.cloud.bootstrap.location:" + externalPropertiesPath)
.run();
assertEquals("externalPropertiesInfoName", this.context.getEnvironment()
.getProperty("info.name"));
assertTrue(this.context.getEnvironment().getPropertySources()
.contains("bootstrap"));
assertEquals("externalPropertiesInfoName",
this.context.getEnvironment().getProperty("info.name"));
assertTrue(
this.context.getEnvironment().getPropertySources().contains("bootstrap"));
}
/**
@ -109,8 +109,8 @@ public class BootstrapConfigurationTests { @@ -109,8 +109,8 @@ public class BootstrapConfigurationTests {
this.context = new SpringApplicationBuilder().web(false)
.sources(BareConfiguration.class).run();
assertEquals("bar", this.context.getEnvironment().getProperty("bootstrap.foo"));
assertTrue(this.context.getEnvironment().getPropertySources()
.contains("bootstrap"));
assertTrue(
this.context.getEnvironment().getPropertySources().contains("bootstrap"));
}
@Test
@ -133,19 +133,20 @@ public class BootstrapConfigurationTests { @@ -133,19 +133,20 @@ public class BootstrapConfigurationTests {
@Test
public void systemPropertyOverrideFalse() {
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
PropertySourceConfiguration.MAP.put(
"spring.cloud.config.overrideSystemProperties", "false");
PropertySourceConfiguration.MAP
.put("spring.cloud.config.overrideSystemProperties", "false");
System.setProperty("bootstrap.foo", "system");
this.context = new SpringApplicationBuilder().web(false)
.sources(BareConfiguration.class).run();
assertEquals("system", this.context.getEnvironment().getProperty("bootstrap.foo"));
assertEquals("system",
this.context.getEnvironment().getProperty("bootstrap.foo"));
}
@Test
public void systemPropertyOverrideWhenOverrideDisallowed() {
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
PropertySourceConfiguration.MAP.put(
"spring.cloud.config.overrideSystemProperties", "false");
PropertySourceConfiguration.MAP
.put("spring.cloud.config.overrideSystemProperties", "false");
// If spring.cloud.config.allowOverride=false is in the remote property sources
// with sufficiently high priority it always wins. Admins can enforce it by adding
// their own remote property source.
@ -159,13 +160,14 @@ public class BootstrapConfigurationTests { @@ -159,13 +160,14 @@ public class BootstrapConfigurationTests {
@Test
public void systemPropertyOverrideFalseWhenOverrideAllowed() {
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
PropertySourceConfiguration.MAP.put(
"spring.cloud.config.overrideSystemProperties", "false");
PropertySourceConfiguration.MAP
.put("spring.cloud.config.overrideSystemProperties", "false");
PropertySourceConfiguration.MAP.put("spring.cloud.config.allowOverride", "true");
System.setProperty("bootstrap.foo", "system");
this.context = new SpringApplicationBuilder().web(false)
.sources(BareConfiguration.class).run();
assertEquals("system", this.context.getEnvironment().getProperty("bootstrap.foo"));
assertEquals("system",
this.context.getEnvironment().getProperty("bootstrap.foo"));
}
@Test
@ -174,9 +176,8 @@ public class BootstrapConfigurationTests { @@ -174,9 +176,8 @@ public class BootstrapConfigurationTests {
PropertySourceConfiguration.MAP.put("spring.cloud.config.overrideNone", "true");
PropertySourceConfiguration.MAP.put("spring.cloud.config.allowOverride", "true");
ConfigurableEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addLast(
new MapPropertySource("last", Collections.<String, Object> singletonMap(
"bootstrap.foo", "splat")));
environment.getPropertySources().addLast(new MapPropertySource("last",
Collections.<String, Object> singletonMap("bootstrap.foo", "splat")));
this.context = new SpringApplicationBuilder().web(false).environment(environment)
.sources(BareConfiguration.class).run();
assertEquals("splat", this.context.getEnvironment().getProperty("bootstrap.foo"));
@ -185,22 +186,18 @@ public class BootstrapConfigurationTests { @@ -185,22 +186,18 @@ public class BootstrapConfigurationTests {
@Test
public void applicationNameInBootstrapAndMain() {
System.setProperty("expected.name", "main");
this.context = new SpringApplicationBuilder()
.web(false)
this.context = new SpringApplicationBuilder().web(false)
.properties("spring.cloud.bootstrap.name:other",
"spring.config.name:plain").sources(BareConfiguration.class)
.run();
"spring.config.name:plain")
.sources(BareConfiguration.class).run();
assertEquals("app",
this.context.getEnvironment().getProperty("spring.application.name"));
// The parent is called "main" because spring.application.name is specified in
// other.properties (the bootstrap properties)
assertEquals(
"main",
this.context.getParent().getEnvironment()
.getProperty("spring.application.name"));
// The bootstrap context has a different "bootstrap" property source
assertNotSame(
this.context.getEnvironment().getPropertySources().get("bootstrap"),
assertEquals("main", this.context.getParent().getEnvironment()
.getProperty("spring.application.name"));
// The bootstrap context has the same "bootstrap" property source
assertEquals(this.context.getEnvironment().getPropertySources().get("bootstrap"),
((ConfigurableEnvironment) this.context.getParent().getEnvironment())
.getPropertySources().get("bootstrap"));
assertEquals("app", this.context.getId());
@ -209,11 +206,10 @@ public class BootstrapConfigurationTests { @@ -209,11 +206,10 @@ public class BootstrapConfigurationTests {
@Test
public void applicationNameNotInBootstrap() {
System.setProperty("expected.name", "main");
this.context = new SpringApplicationBuilder()
.web(false)
this.context = new SpringApplicationBuilder().web(false)
.properties("spring.cloud.bootstrap.name:application",
"spring.config.name:other").sources(BareConfiguration.class)
.run();
"spring.config.name:other")
.sources(BareConfiguration.class).run();
assertEquals("main",
this.context.getEnvironment().getProperty("spring.application.name"));
// The parent is called "application" because spring.application.name is not
@ -234,10 +230,8 @@ public class BootstrapConfigurationTests { @@ -234,10 +230,8 @@ public class BootstrapConfigurationTests {
this.context.getEnvironment().getProperty("spring.application.name"));
// The parent is called "main" because spring.application.name is specified in
// other.properties (the bootstrap properties this time)
assertEquals(
"main",
this.context.getParent().getEnvironment()
.getProperty("spring.application.name"));
assertEquals("main", this.context.getParent().getEnvironment()
.getProperty("spring.application.name"));
assertEquals("main", this.context.getId());
}
@ -248,8 +242,8 @@ public class BootstrapConfigurationTests { @@ -248,8 +242,8 @@ public class BootstrapConfigurationTests {
.environment(new StandardEnvironment()).child(BareConfiguration.class)
.web(false).run();
assertEquals("bar", this.context.getEnvironment().getProperty("bootstrap.foo"));
assertEquals(this.context.getEnvironment(), this.context.getParent()
.getEnvironment());
assertEquals(this.context.getEnvironment(),
this.context.getParent().getEnvironment());
MutablePropertySources sources = this.context.getEnvironment()
.getPropertySources();
PropertySource<?> bootstrap = sources.get("bootstrap");
@ -263,10 +257,10 @@ public class BootstrapConfigurationTests { @@ -263,10 +257,10 @@ public class BootstrapConfigurationTests {
this.context = new SpringApplicationBuilder().sources(BareConfiguration.class)
.child(BareConfiguration.class).web(false).run();
assertEquals("bar", this.context.getEnvironment().getProperty("bootstrap.foo"));
assertNotSame(this.context.getEnvironment(), this.context.getParent()
.getEnvironment());
assertTrue(this.context.getEnvironment().getPropertySources()
.contains("bootstrap"));
assertNotSame(this.context.getEnvironment(),
this.context.getParent().getEnvironment());
assertTrue(
this.context.getEnvironment().getPropertySources().contains("bootstrap"));
assertTrue(((ConfigurableEnvironment) this.context.getParent().getEnvironment())
.getPropertySources().contains("bootstrap"));
}
@ -279,8 +273,8 @@ public class BootstrapConfigurationTests { @@ -279,8 +273,8 @@ public class BootstrapConfigurationTests {
.sources(BareConfiguration.class).profiles("parent").web(false).run();
this.context = new SpringApplicationBuilder(BareConfiguration.class)
.profiles("child").parent(parent).web(false).run();
assertNotSame(this.context.getEnvironment(), this.context.getParent()
.getEnvironment());
assertNotSame(this.context.getEnvironment(),
this.context.getParent().getEnvironment());
// The ApplicationContext merges profiles (profiles and property sources), see
// AbstractEnvironment.merge()
assertTrue(this.context.getEnvironment().acceptsProfiles("child", "parent"));

Loading…
Cancel
Save