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 4f22eb26..0b156443 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 @@ -20,6 +20,8 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +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; @@ -97,6 +99,9 @@ public class ConfigurationPropertiesRebinder if (this.applicationContext != null) { try { Object bean = this.applicationContext.getBean(name); + if (AopUtils.isCglibProxy(bean)) { + bean = getTargetObject(bean); + } this.binder.postProcessBeforeInitialization(bean, name); this.applicationContext.getAutowireCapableBeanFactory() .initializeBean(bean, name); @@ -110,6 +115,19 @@ public class ConfigurationPropertiesRebinder return false; } + @SuppressWarnings("unchecked") + private static T getTargetObject(Object candidate) { + try { + if (AopUtils.isAopProxy(candidate) && (candidate instanceof Advised)) { + return (T) ((Advised) candidate).getTargetSource().getTarget(); + } + } + catch (Exception ex) { + throw new IllegalStateException("Failed to unwrap proxied object", ex); + } + return (T) candidate; + } + @ManagedAttribute public Set getBeanNames() { return new HashSet(this.beans.getBeanNames()); diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderProxyIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderProxyIntegrationTests.java new file mode 100644 index 00000000..a973fd49 --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderProxyIntegrationTests.java @@ -0,0 +1,120 @@ +/* + * Copyright 2006-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.context.properties; + +import java.util.HashMap; +import java.util.Map; + +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration; +import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderProxyIntegrationTests.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.assertEquals; + +@SpringApplicationConfiguration(classes = TestConfiguration.class) +@RunWith(SpringJUnit4ClassRunner.class) +@IntegrationTest({ "messages.expiry.one=168", "messages.expiry.two=76" }) +public class ConfigurationPropertiesRebinderProxyIntegrationTests { + + @Autowired + private TestProperties properties; + + @Autowired + private ConfigurationPropertiesRebinder rebinder; + + @Autowired + private ConfigurableEnvironment environment; + + @Test + @DirtiesContext + public void testAppendProperties() throws Exception { + // This comes out as a String not Integer if the rebinder processes the proxy + // instead of the target + assertEquals(new Integer(168), this.properties.getExpiry().get("one")); + EnvironmentTestUtils.addEnvironment(this.environment, "messages.expiry.one=56"); + this.rebinder.rebind(); + assertEquals(new Integer(56), this.properties.getExpiry().get("one")); + } + + @Configuration + @EnableConfigurationProperties + @Import({ Interceptor.class, RefreshConfiguration.RebinderConfiguration.class, + PropertyPlaceholderAutoConfiguration.class, AopAutoConfiguration.class }) + protected static class TestConfiguration { + + @Bean + protected TestProperties properties() { + return new TestProperties(); + } + + } + + @Aspect + protected static class Interceptor { + @Before("execution(* *..TestProperties.*(..))") + public void before() { + System.err.println("Before"); + } + } + + // Hack out a protected inner class for testing + protected static class RefreshConfiguration extends RefreshAutoConfiguration { + @Configuration + protected static class RebinderConfiguration + extends ConfigurationPropertiesRebinderAutoConfiguration { + + } + } + + @ConfigurationProperties("messages") + protected static class TestProperties { + private String name; + + private final Map expiry = new HashMap<>(); + + public Map getExpiry() { + return expiry; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + +}