Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
this.applicationContext.getAutowireCapableBeanFactory().destroyBean(bean);
|
||||
this.applicationContext.getAutowireCapableBeanFactory().initializeBean(bean, name);
|
||||
return true;
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user