From 86c44de8553f2f655068e6591336e295167656be Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Tue, 25 Oct 2022 09:05:18 -0400 Subject: [PATCH] Destroy and initialize beans within the correct context. Fixes #1158 (#1160) Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com> --- .../ConfigurationPropertiesRebinder.java | 49 +++++++++++-------- ...ionPropertiesRebinderIntegrationTests.java | 4 +- 2 files changed, 30 insertions(+), 23 deletions(-) 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 fc66a7cc..2f83e237 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 @@ -89,30 +89,37 @@ public class ConfigurationPropertiesRebinder if (!this.beans.getBeanNames().contains(name)) { return false; } - if (this.applicationContext != null) { - try { - Object bean = this.applicationContext.getBean(name); - if (AopUtils.isAopProxy(bean)) { - bean = ProxyUtils.getTargetObject(bean); - } - if (bean != null) { - // TODO: determine a more general approach to fix this. - // see https://github.com/spring-cloud/spring-cloud-commons/issues/571 - if (getNeverRefreshable().contains(bean.getClass().getName())) { - return false; // ignore + ApplicationContext appContext = this.applicationContext; + while (appContext != null) { + if (appContext.containsLocalBean(name)) { + try { + Object bean = appContext.getBean(name); + if (AopUtils.isAopProxy(bean)) { + bean = ProxyUtils.getTargetObject(bean); + } + if (bean != null) { + // TODO: determine a more general approach to fix this. + // see + // https://github.com/spring-cloud/spring-cloud-commons/issues/571 + if (getNeverRefreshable().contains(bean.getClass().getName())) { + return false; // ignore + } + appContext.getAutowireCapableBeanFactory().destroyBean(bean); + appContext.getAutowireCapableBeanFactory().initializeBean(bean, name); + return true; } - this.applicationContext.getAutowireCapableBeanFactory().destroyBean(bean); - this.applicationContext.getAutowireCapableBeanFactory().initializeBean(bean, name); - return true; + } + catch (RuntimeException e) { + this.errors.put(name, e); + throw e; + } + catch (Exception e) { + this.errors.put(name, e); + throw new IllegalStateException("Cannot rebind to " + name, e); } } - catch (RuntimeException e) { - this.errors.put(name, e); - throw e; - } - catch (Exception e) { - this.errors.put(name, e); - throw new IllegalStateException("Cannot rebind to " + name, e); + else { + appContext = appContext.getParent(); } } return false; diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderIntegrationTests.java index a9b3c49c..82dd040e 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderIntegrationTests.java @@ -71,12 +71,12 @@ public class ConfigurationPropertiesRebinderIntegrationTests { @Test @DirtiesContext public void testRefreshInParent() throws Exception { - then(this.config.getName()).isEqualTo("main"); + then(this.config.getName()).isEqualTo("parent"); // Change the dynamic property source... TestPropertyValues.of("config.name=foo").applyTo(this.environment); // ...and then refresh, so the bean is re-initialized: this.rebinder.rebind(); - then(this.config.getName()).isEqualTo("foo"); + then(this.config.getName()).isEqualTo("parent"); } @Test