From b628338e54f638aee1913956c154a73a435e52df Mon Sep 17 00:00:00 2001 From: "o.gusev" Date: Thu, 18 Feb 2021 11:56:05 +0300 Subject: [PATCH] Fixes gh-898 If HikariDataSource bean gets AOP-proxied more than once, the spring-cloud rebind operation doesn't treat it as never-refreshable. --- .../ConfigurationPropertiesRebinder.java | 2 +- .../springframework/cloud/util/ProxyUtils.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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 f3f766fe..657c3a5b 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 @@ -94,7 +94,7 @@ public class ConfigurationPropertiesRebinder try { Object bean = this.applicationContext.getBean(name); if (AopUtils.isAopProxy(bean)) { - bean = ProxyUtils.getTargetObject(bean); + bean = ProxyUtils.getUltimateTargetObject(bean); } if (bean != null) { // TODO: determine a more general approach to fix this. diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/util/ProxyUtils.java b/spring-cloud-context/src/main/java/org/springframework/cloud/util/ProxyUtils.java index c4058d75..2558d05f 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/util/ProxyUtils.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/util/ProxyUtils.java @@ -18,6 +18,7 @@ package org.springframework.cloud.util; import org.springframework.aop.framework.Advised; import org.springframework.aop.support.AopUtils; +import org.springframework.util.Assert; /** * @author Ryan Baxter @@ -41,4 +42,21 @@ public final class ProxyUtils { return (T) candidate; } + @SuppressWarnings("unchecked") + public static T getUltimateTargetObject(Object candidate) { + Assert.notNull(candidate, "Candidate must not be null"); + try { + if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised) { + Object target = ((Advised) candidate).getTargetSource().getTarget(); + if (target != null) { + return (T) getUltimateTargetObject(target); + } + } + } + catch (Exception ex) { + throw new IllegalStateException("Failed to unwrap proxied object", ex); + } + return (T) candidate; + } + }