Skip to content

Commit c73c01c

Browse files
Prevent nested proxies and improve error safety in rebind
- Unwrap freshBean if createBean() returns an AOP proxy to avoid double-proxied targets where advice executes twice - Create fresh instance before destroying old target so a failure in createBean()/setTargetSource() does not leave the proxy pointing at an already-destroyed target - On setTargetSource failure, destroy freshBean before rethrowing Signed-off-by: seonghyeoklee <dltjdgur327@gmail.com>
1 parent 4b14063 commit c73c01c

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,22 @@ private boolean rebind(String name, ApplicationContext appContext) {
140140
|| getNeverRefreshable().contains(name)) {
141141
return false; // ignore
142142
}
143-
appContext.getAutowireCapableBeanFactory().destroyBean(target);
144143
if (proxied && bean instanceof Advised advised) {
145144
Object freshBean = appContext.getAutowireCapableBeanFactory().createBean(target.getClass());
146-
advised.setTargetSource(new org.springframework.aop.target.SingletonTargetSource(freshBean));
145+
if (AopUtils.isAopProxy(freshBean)) {
146+
freshBean = ProxyUtils.getTargetObject(freshBean);
147+
}
148+
try {
149+
advised.setTargetSource(new org.springframework.aop.target.SingletonTargetSource(freshBean));
150+
}
151+
catch (Exception ex) {
152+
appContext.getAutowireCapableBeanFactory().destroyBean(freshBean);
153+
throw ex;
154+
}
155+
appContext.getAutowireCapableBeanFactory().destroyBean(target);
147156
}
148157
else {
158+
appContext.getAutowireCapableBeanFactory().destroyBean(target);
149159
appContext.getAutowireCapableBeanFactory().initializeBean(target, name);
150160
}
151161
return true;

0 commit comments

Comments
 (0)