From c5b9a9f3e76f58d10c3010d86982b6e9485a6768 Mon Sep 17 00:00:00 2001 From: Marius Bogoevici Date: Tue, 13 Dec 2016 20:19:45 -0500 Subject: [PATCH] Refine the behaviour of the defaults map Fixes #734 - allow mutability - use Environment directly for configuration Signed-off-by: Marius Bogoevici --- .../config/BindingServiceProperties.java | 22 +++++++++----- .../EnvironmentEntryInitializingTreeMap.java | 30 ++++++++++--------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceProperties.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceProperties.java index 6c64813ba..a36a223a7 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceProperties.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceProperties.java @@ -33,7 +33,10 @@ import org.springframework.cloud.stream.binder.ProducerProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.EnvironmentAware; import org.springframework.core.convert.ConversionService; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; import org.springframework.integration.support.utils.IntegrationUtils; import org.springframework.util.Assert; @@ -45,8 +48,7 @@ import org.springframework.util.Assert; */ @ConfigurationProperties("spring.cloud.stream") @JsonInclude(Include.NON_DEFAULT) -public class BindingServiceProperties - implements ApplicationContextAware, InitializingBean { +public class BindingServiceProperties implements ApplicationContextAware, EnvironmentAware, InitializingBean { private ConversionService conversionService; @@ -118,11 +120,17 @@ public class BindingServiceProperties public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = (ConfigurableApplicationContext) applicationContext; - // override the bindings store with the environment-initializing version if in a - // Spring context - this.bindings = new EnvironmentEntryInitializingTreeMap<>(this.applicationContext, - BindingProperties.class, "spring.cloud.stream.default", - new TreeMap(String.CASE_INSENSITIVE_ORDER)); + } + + @Override + public void setEnvironment(Environment environment) { + if (environment instanceof ConfigurableEnvironment) { + // override the bindings store with the environment-initializing version if in + // a Spring context + this.bindings = new EnvironmentEntryInitializingTreeMap<>((ConfigurableEnvironment) environment, + BindingProperties.class, "spring.cloud.stream.default", + new TreeMap(String.CASE_INSENSITIVE_ORDER)); + } } public void setConversionService(ConversionService conversionService) { diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/EnvironmentEntryInitializingTreeMap.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/EnvironmentEntryInitializingTreeMap.java index d35ef9517..44b8d7fd8 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/EnvironmentEntryInitializingTreeMap.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/EnvironmentEntryInitializingTreeMap.java @@ -23,14 +23,14 @@ import java.util.Set; import org.springframework.beans.BeanUtils; import org.springframework.boot.bind.PropertySourcesPropertyValues; import org.springframework.boot.bind.RelaxedDataBinder; -import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.util.Assert; /** * A {@link Map} implementation that initializes its entries by binding values from the - * supplied application context's environment. Any call to 'get()' will result in either - * returning the existing value or initializing a new entry by binding properties with the - * specified prefix from the environment. + * supplied environment. Any call to 'get()' will result in either returning the existing + * value or initializing a new entry by binding properties with the specified prefix from + * the environment. * * This is strictly intended to be used for configuration property values and not to be * used as a general purpose map. @@ -41,7 +41,7 @@ import org.springframework.util.Assert; */ public class EnvironmentEntryInitializingTreeMap extends AbstractMap { - private final ConfigurableApplicationContext applicationContext; + private final ConfigurableEnvironment environment; private final Class entryClass; @@ -52,18 +52,18 @@ public class EnvironmentEntryInitializingTreeMap extends AbstractMap entryClass, + public EnvironmentEntryInitializingTreeMap(ConfigurableEnvironment environment, Class entryClass, String defaultsPrefix, Map delegate) { - Assert.notNull(applicationContext, "The context cannot be null"); + Assert.notNull(environment, "The environment cannot be null"); Assert.notNull(entryClass, "The entry class cannot be null"); Assert.notNull(defaultsPrefix, "The prefix for the property defaults cannot be null"); Assert.notNull(delegate, "The delegate cannot be null"); - this.applicationContext = applicationContext; + this.environment = environment; this.entryClass = entryClass; this.defaultsPrefix = defaultsPrefix; this.delegate = delegate; @@ -73,16 +73,18 @@ public class EnvironmentEntryInitializingTreeMap extends AbstractMap> entrySet() { return delegate.entrySet();