Merge branch 'master' into 2.0.x

This commit is contained in:
Dave Syer
2017-11-03 14:12:27 +00:00
3 changed files with 20 additions and 12 deletions

View File

@@ -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<String> 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());

View File

@@ -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");

View File

@@ -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<String> 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<String> 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<String> keys = endpoint.refresh();