From fb37883a9266773412d8b6bcdc529d3e91acd5ba Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Sat, 30 Jan 2016 10:20:49 +0000 Subject: [PATCH] Ensure BootstrapApplicationListener adds property sources on refresh The fact that PropertySourceBootstrapConfiguration was re-using the "bootstrap" property source name from the listener was a problem. The listener has to be able to manage its own temporary property source name. Fixes gh-83 --- .../PropertySourceBootstrapConfiguration.java | 10 +++--- .../cloud/endpoint/RefreshEndpointTests.java | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) 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 92585442..bd3ba86f 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 @@ -53,10 +53,11 @@ import org.springframework.util.ResourceUtils; */ @Configuration @EnableConfigurationProperties(PropertySourceBootstrapProperties.class) -public class PropertySourceBootstrapConfiguration - implements ApplicationContextInitializer, Ordered { +public class PropertySourceBootstrapConfiguration implements + ApplicationContextInitializer, Ordered { - private static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME; + private static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME + + "Properties"; private static Log logger = LogFactory .getLog(PropertySourceBootstrapConfiguration.class); @@ -108,7 +109,8 @@ public class PropertySourceBootstrapConfiguration private void reinitializeLoggingSystem(ConfigurableEnvironment environment, String oldLogConfig, LogFile oldLogFile) { - Map props = new RelaxedPropertyResolver(environment).getSubProperties("logging."); + Map props = new RelaxedPropertyResolver(environment) + .getSubProperties("logging."); if (!props.isEmpty()) { String logConfig = environment.resolvePlaceholders("${logging.config:}"); LogFile logFile = LogFile.get(environment); 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 af345e0f..a9a82d25 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 @@ -19,6 +19,7 @@ package org.springframework.cloud.endpoint; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -27,6 +28,7 @@ import org.junit.Test; import org.springframework.boot.Banner.Mode; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent; @@ -34,6 +36,10 @@ import org.springframework.context.ApplicationEvent; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.EventListener; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.stereotype.Component; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; @@ -82,6 +88,20 @@ public class RefreshEndpointTests { assertTrue("Wrong keys: " + keys, keys.contains("message")); } + @Test + public void keysComputedWhenChangesInExternalProperties() throws Exception { + this.context = new SpringApplicationBuilder(Empty.class).web(false) + .bannerMode(Mode.OFF).properties("spring.cloud.bootstrap.name:none") + .run(); + RefreshScope scope = new RefreshScope(); + scope.setApplicationContext(this.context); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.main.sources=" + ExternalPropertySourceLocator.class.getName()); + RefreshEndpoint endpoint = new RefreshEndpoint(this.context, scope); + Collection keys = endpoint.invoke(); + assertTrue("Wrong keys: " + keys, keys.contains("external.message")); + } + @Test public void eventsPublishedInOrder() throws Exception { this.context = new SpringApplicationBuilder(Empty.class).web(false) @@ -133,4 +153,16 @@ public class RefreshEndpointTests { this.events.add(event); } } + + @Component + protected static class ExternalPropertySourceLocator + implements PropertySourceLocator { + + @Override + public PropertySource locate(Environment environment) { + return new MapPropertySource("external", Collections + . singletonMap("external.message", "I'm External")); + } + + } }