Browse Source

Add flag spring.cloud.config.overrideNone

If true (and spring.cloud.config.allowOverride=true) then the bootstrap
property source is added *last*, so all other property sources can override
it (including local config files).

Fixes gh-26, fixes gh-27.
pull/30/head
Dave Syer 10 years ago
parent
commit
9d4584bb5c
  1. 20
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java
  2. 19
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapProperties.java
  3. 26
      spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java

20
spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java

@ -134,15 +134,27 @@ public class PropertySourceBootstrapConfiguration implements @@ -134,15 +134,27 @@ public class PropertySourceBootstrapConfiguration implements
new RelaxedDataBinder(remoteProperties, "spring.cloud.config")
.bind(new PropertySourcesPropertyValues(incoming));
if (!remoteProperties.isAllowOverride()
|| remoteProperties.isOverrideSystemProperties()) {
|| (!remoteProperties.isOverrideNone() && remoteProperties
.isOverrideSystemProperties())) {
propertySources.addFirst(composite);
return;
}
if (remoteProperties.isOverrideNone()) {
propertySources.addLast(composite);
return;
}
if (propertySources
.contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
propertySources.addAfter(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
composite);
if (!remoteProperties.isOverrideSystemProperties()) {
propertySources.addAfter(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
composite);
}
else {
propertySources.addBefore(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
composite);
}
}
else {
propertySources.addLast(composite);

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

@ -18,8 +18,23 @@ public class PropertySourceBootstrapProperties { @@ -18,8 +18,23 @@ public class PropertySourceBootstrapProperties {
*/
private boolean allowOverride = true;
/**
* Flag to indicate that when {@link #setAllowOverride(boolean) allowOverride} is
* true, external properties should take lowest priority, and not override any
* existing property sources (including local config files). Default false.
*/
private boolean overrideNone = false;
public boolean isOverrideNone() {
return this.overrideNone;
}
public void setOverrideNone(boolean overrideNone) {
this.overrideNone = overrideNone;
}
public boolean isOverrideSystemProperties() {
return overrideSystemProperties;
return this.overrideSystemProperties;
}
public void setOverrideSystemProperties(boolean overrideSystemProperties) {
@ -27,7 +42,7 @@ public class PropertySourceBootstrapProperties { @@ -27,7 +42,7 @@ public class PropertySourceBootstrapProperties {
}
public boolean isAllowOverride() {
return allowOverride;
return this.allowOverride;
}
public void setAllowOverride(boolean allowOverride) {

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

@ -156,6 +156,32 @@ public class BootstrapConfigurationTests { @@ -156,6 +156,32 @@ public class BootstrapConfigurationTests {
assertEquals("bar", this.context.getEnvironment().getProperty("bootstrap.foo"));
}
@Test
public void systemPropertyOverrideFalseWhenOverrideAllowed() {
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
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"));
}
@Test
public void overrideAllWhenOverrideAllowed() {
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
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")));
this.context = new SpringApplicationBuilder().web(false).environment(environment)
.sources(BareConfiguration.class).run();
assertEquals("splat", this.context.getEnvironment().getProperty("bootstrap.foo"));
}
@Test
public void applicationNameInBootstrapAndMain() {
System.setProperty("expected.name", "main");

Loading…
Cancel
Save