diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java index 909988c7..d9e2c6b8 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java @@ -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); diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapProperties.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapProperties.java index cfe2692b..bcda496d 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapProperties.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapProperties.java @@ -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 { } public boolean isAllowOverride() { - return allowOverride; + return this.allowOverride; } public void setAllowOverride(boolean allowOverride) { 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 c40022d6..8f7b3e57 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 @@ -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. 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");