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 e4979db2..acdab8d8 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 @@ -33,7 +33,7 @@ public class ContextRefresher { private static final String REFRESH_ARGS_PROPERTY_SOURCE = "refreshArgs"; - private Set standardSources = new HashSet( + private Set standardSources = new HashSet<>( Arrays.asList(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME, @@ -59,7 +59,7 @@ public class ContextRefresher { return keys; } - private void addConfigFilesToEnvironment() { + /* for testing */ ConfigurableApplicationContext addConfigFilesToEnvironment() { ConfigurableApplicationContext capture = null; try { StandardEnvironment environment = copyEnvironment( @@ -117,7 +117,7 @@ public class ContextRefresher { } } } - + return capture; } // Don't use ConfigurableEnvironment.merge() in case there are clashes with property 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 1c1a0251..96e7dd52 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 @@ -71,6 +71,22 @@ public class ContextRefresherTests { assertThat(names).first().isEqualTo("bootstrapProperties"); } + @Test + public void parentContextIsClosed() { + // Use spring.cloud.bootstrap.name to switch off the defaults (which would pick up + // a bootstrapProperties immediately + context = SpringApplication.run(ContextRefresherTests.class, + "--spring.main.webEnvironment=false", "--debug=false", + "--spring.main.bannerMode=OFF", "--spring.cloud.bootstrap.name=refresh"); + ContextRefresher refresher = new ContextRefresher(context, scope); + EnvironmentTestUtils.addEnvironment(context, + "spring.cloud.bootstrap.sources: org.springframework.cloud.context.refresh.ContextRefresherTests.PropertySourceConfiguration\n" + + ""); + ConfigurableApplicationContext refresherContext = refresher.addConfigFilesToEnvironment(); + assertThat(refresherContext.getParent()).isNotNull().isInstanceOf(ConfigurableApplicationContext.class); + ConfigurableApplicationContext parent = (ConfigurableApplicationContext) refresherContext.getParent(); + assertThat(parent.isActive()).isFalse(); + } private List names(MutablePropertySources propertySources) { List list = new ArrayList<>(); for (PropertySource p : propertySources) {