Browse Source

Merge branch 'master' into 2.0.x

pull/284/head
Dave Syer 7 years ago
parent
commit
0d16cf0f83
  1. 22
      spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java
  2. 3
      spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java
  3. 7
      spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java

22
spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java

@ -34,6 +34,9 @@ public class ContextRefresher { @@ -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 { @@ -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());

3
spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java

@ -12,6 +12,7 @@ import org.junit.Test; @@ -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 { @@ -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");

7
spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java

@ -33,6 +33,7 @@ import org.springframework.boot.Banner.Mode; @@ -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 { @@ -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 { @@ -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 { @@ -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();

Loading…
Cancel
Save