diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java index 35d3c8cc..72103ce2 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java @@ -34,6 +34,9 @@ public class ContextRefresher { private static final String REFRESH_ARGS_PROPERTY_SOURCE = "refreshArgs"; + private static final String[] DEFAULT_PROPERTY_SOURCES = new String[] { + "defaultProperties" }; + private Set standardSources = new HashSet<>( Arrays.asList(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, @@ -128,15 +131,18 @@ public class ContextRefresher { private StandardEnvironment copyEnvironment(ConfigurableEnvironment input) { StandardEnvironment environment = new StandardEnvironment(); MutablePropertySources capturedPropertySources = environment.getPropertySources(); - for (PropertySource source : capturedPropertySources) { - capturedPropertySources.remove(source.getName()); - } - for (PropertySource source : input.getPropertySources()) { - if ("configurationProperties".equals(source.getName())) { - // Spring Boot artifact, tracking values in the other sources - continue; + // Only copy the default property source(s) and the profiles over from the main + // environment (everything else should be pristine, just like it was on startup). + for (String name : DEFAULT_PROPERTY_SOURCES) { + if (input.getPropertySources().contains(name)) { + if (capturedPropertySources.contains(name)) { + capturedPropertySources.replace(name, + input.getPropertySources().get(name)); + } + else { + capturedPropertySources.addLast(input.getPropertySources().get(name)); + } } - capturedPropertySources.addLast(source); } environment.setActiveProfiles(input.getActiveProfiles()); environment.setDefaultProfiles(input.getDefaultProfiles()); diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java index a7bea02b..0fd34599 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java @@ -12,6 +12,7 @@ import org.junit.Test; import org.mockito.Mockito; import org.springframework.boot.SpringApplication; import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.boot.test.util.TestPropertyValues.Type; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.context.ConfigurableApplicationContext; @@ -59,7 +60,7 @@ public class ContextRefresherTests { ContextRefresher refresher = new ContextRefresher(context, scope); TestPropertyValues.of( "spring.cloud.bootstrap.sources: org.springframework.cloud.context.refresh.ContextRefresherTests.PropertySourceConfiguration") - .applyTo(context); + .applyTo(context.getEnvironment(), Type.MAP, "defaultProperties"); refresher.refresh(); names = names(context.getEnvironment().getPropertySources()); assertThat(names).first().isEqualTo("bootstrapProperties"); diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java index 80b79276..5be7da34 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java @@ -33,6 +33,7 @@ import org.springframework.boot.Banner.Mode; import org.springframework.boot.WebApplicationType; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.boot.test.util.TestPropertyValues.Type; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.cloud.context.refresh.ContextRefresher; @@ -71,7 +72,7 @@ public class RefreshEndpointTests { .properties("spring.cloud.bootstrap.name:none").run(); RefreshScope scope = new RefreshScope(); scope.setApplicationContext(this.context); - TestPropertyValues.of("spring.profiles.active=local").applyTo(this.context); + context.getEnvironment().setActiveProfiles("local"); ContextRefresher contextRefresher = new ContextRefresher(this.context, scope); RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher); Collection keys = endpoint.refresh(); @@ -85,7 +86,7 @@ public class RefreshEndpointTests { .properties("spring.cloud.bootstrap.name:none").run(); RefreshScope scope = new RefreshScope(); scope.setApplicationContext(this.context); - TestPropertyValues.of("spring.profiles.active=override").applyTo(this.context); + context.getEnvironment().setActiveProfiles("override"); ContextRefresher contextRefresher = new ContextRefresher(this.context, scope); RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher); Collection keys = endpoint.refresh(); @@ -102,7 +103,7 @@ public class RefreshEndpointTests { TestPropertyValues .of("spring.cloud.bootstrap.sources=" + ExternalPropertySourceLocator.class.getName()) - .applyTo(this.context); + .applyTo(this.context.getEnvironment(), Type.MAP, "defaultProperties"); ContextRefresher contextRefresher = new ContextRefresher(this.context, scope); RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher); Collection keys = endpoint.refresh();