Browse Source

Destroy and initialize beans within the correct context. Fixes #1158 (#1160)

Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
pull/1163/head
Ryan Baxter 2 years ago committed by GitHub
parent
commit
86c44de855
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 49
      spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java
  2. 4
      spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderIntegrationTests.java

49
spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java

@ -89,30 +89,37 @@ public class ConfigurationPropertiesRebinder @@ -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;

4
spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderIntegrationTests.java

@ -71,12 +71,12 @@ public class ConfigurationPropertiesRebinderIntegrationTests { @@ -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

Loading…
Cancel
Save