diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.java index fae23c21..19281bd2 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.java @@ -64,10 +64,8 @@ public class ConfigurationPropertiesRebinderAutoConfiguration @Bean @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) public ConfigurationPropertiesRebinder configurationPropertiesRebinder( - ConfigurationPropertiesBeans beans, - ConfigurationPropertiesBindingPostProcessor binder) { - ConfigurationPropertiesRebinder rebinder = new ConfigurationPropertiesRebinder( - binder, beans); + ConfigurationPropertiesBeans beans) { + ConfigurationPropertiesRebinder rebinder = new ConfigurationPropertiesRebinder(beans); return rebinder; } diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java index 14a137a6..fc6cf1f5 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java @@ -15,28 +15,32 @@ */ package org.springframework.cloud.context.properties; -import java.lang.reflect.Field; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.springframework.aop.framework.Advised; import org.springframework.aop.support.AopUtils; import org.springframework.beans.BeansException; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; +import org.springframework.boot.context.properties.ConfigurationPropertiesBinder; +import org.springframework.boot.context.properties.ConfigurationPropertiesBinderBuilder; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; +import org.springframework.core.env.StandardEnvironment; import org.springframework.jmx.export.annotation.ManagedAttribute; import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.stereotype.Component; -import org.springframework.util.ReflectionUtils; /** * Listens for {@link EnvironmentChangeEvent} and rebinds beans that were bound to the @@ -55,9 +59,12 @@ import org.springframework.util.ReflectionUtils; public class ConfigurationPropertiesRebinder implements ApplicationContextAware, ApplicationListener { + private static final Log log = LogFactory + .getLog(ConfigurationPropertiesRebinder.class); + private ConfigurationPropertiesBeans beans; - private ConfigurationPropertiesBindingPostProcessor binder; + private ConfigurationPropertiesBinder binder; private ApplicationContext applicationContext; @@ -65,13 +72,29 @@ public class ConfigurationPropertiesRebinder private boolean resetting = false; - public ConfigurationPropertiesRebinder( - ConfigurationPropertiesBindingPostProcessor binder, - ConfigurationPropertiesBeans beans) { - this.binder = binder; + public ConfigurationPropertiesRebinder(ConfigurationPropertiesBeans beans) { this.beans = beans; } + private ConfigurationPropertiesBinder getBinder() { + if (this.binder == null) { + ConfigurableEnvironment environment = null; + Environment object = this.applicationContext.getEnvironment(); + if (!(object instanceof ConfigurableEnvironment)) { + log.warn("Environment is not of type ConfigurableEnvironment"); + environment = new StandardEnvironment(); + } + else { + environment = (ConfigurableEnvironment) object; + } + this.binder = new ConfigurationPropertiesBinderBuilder( + this.applicationContext) + .withPropertySources(environment.getPropertySources()) + .build(); + } + return this.binder; + } + @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { @@ -114,7 +137,7 @@ public class ConfigurationPropertiesRebinder if (AopUtils.isCglibProxy(bean)) { bean = getTargetObject(bean); } - this.binder.postProcessBeforeInitialization(bean, name); + getBinder().bind(bean); this.applicationContext.getAutowireCapableBeanFactory() .initializeBean(bean, name); return true; @@ -128,18 +151,7 @@ public class ConfigurationPropertiesRebinder } private void resetBinder() { - try { - setField(binder, "propertySources", null); - binder.afterPropertiesSet(); - } - catch (Exception e) { - } - } - - private void setField(Object target, String name, Object value) { - Field field = ReflectionUtils.findField(target.getClass(), name); - ReflectionUtils.makeAccessible(field); - ReflectionUtils.setField(field, target, value); + this.binder = null; } @SuppressWarnings("unchecked") diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/environment/EnvironmentManagerIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/environment/EnvironmentManagerIntegrationTests.java index 977f83c9..956b08c4 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/environment/EnvironmentManagerIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/environment/EnvironmentManagerIntegrationTests.java @@ -21,9 +21,13 @@ import java.util.Map; import javax.servlet.ServletException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -37,9 +41,6 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;