From c7b907d9cd322d10a1db8df8216845caf1252f5c Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 7 Nov 2017 08:37:44 +0000 Subject: [PATCH] Destroy beans before initializing them Fixes gh-268 --- .../ConfigurationPropertiesRebinder.java | 2 +- ...tiesRebinderLifecycleIntegrationTests.java | 119 ++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderLifecycleIntegrationTests.java 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 b6b9661c..16d68f81 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 @@ -62,7 +62,6 @@ public class ConfigurationPropertiesRebinder this.beans = beans; } - @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { @@ -97,6 +96,7 @@ public class ConfigurationPropertiesRebinder if (AopUtils.isCglibProxy(bean)) { bean = getTargetObject(bean); } + this.applicationContext.getAutowireCapableBeanFactory().destroyBean(bean); this.applicationContext.getAutowireCapableBeanFactory() .initializeBean(bean, name); return true; diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderLifecycleIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderLifecycleIntegrationTests.java new file mode 100644 index 00000000..d8b1c9f7 --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderLifecycleIntegrationTests.java @@ -0,0 +1,119 @@ +/* + * Copyright 2006-2017 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 org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration; +import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderLifecycleIntegrationTests.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.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = TestConfiguration.class) +public class ConfigurationPropertiesRebinderLifecycleIntegrationTests { + + @Autowired + private TestProperties properties; + + @Autowired + private ConfigurationPropertiesRebinder rebinder; + + @Autowired + private ConfigurableEnvironment environment; + + @Test + @DirtiesContext + public void testRefresh() throws Exception { + assertEquals(1, this.properties.getCount()); + assertEquals("Hello scope!", this.properties.getMessage()); + // Change the dynamic property source... + TestPropertyValues.of("message:Foo").applyTo(this.environment); + // ...and then refresh, so the bean is re-initialized: + this.rebinder.rebind(); + assertEquals("Foo", this.properties.getMessage()); + assertEquals(2, this.properties.getCount()); + } + + @Configuration + @EnableConfigurationProperties + @Import({ RefreshConfiguration.RebinderConfiguration.class, + PropertyPlaceholderAutoConfiguration.class }) + protected static class TestConfiguration { + + @Bean + protected TestProperties properties() { + return new TestProperties(); + } + + } + + // Hack out a protected inner class for testing + protected static class RefreshConfiguration extends RefreshAutoConfiguration { + @Configuration + protected static class RebinderConfiguration + extends ConfigurationPropertiesRebinderAutoConfiguration { + + } + } + + @ConfigurationProperties + protected static class TestProperties implements DisposableBean, InitializingBean { + private String message; + private int count = 0; + + public int getCount() { + return this.count; + } + + public String getMessage() { + return this.message; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public void afterPropertiesSet() throws Exception { + assertThat(this.message).isNotEmpty(); + } + + @Override + public void destroy() throws Exception { + this.message = ""; + this.count++; + } + } + +}