From c5f0f7bb11ce54383b960942de298f7d4c9580fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Thu, 30 Mar 2023 15:28:50 +0200 Subject: [PATCH] Refine DisposableBeanAdapter method discovery for native After this commit, DisposableBeanAdapter can find destruction related methods even when hints are just specified at interface level, which is typically the case when a bean is exposed via one of its interfaces. Closes gh-29545 --- .../support/DisposableBeanAdapter.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java index 5e9c6bb04c0..4b02c869cdd 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java @@ -49,6 +49,7 @@ import org.springframework.util.StringUtils; * @author Costin Leau * @author Stephane Nicoll * @author Sam Brannen + * @author Sebastien Deleuze * @since 2.0 * @see AbstractBeanFactory * @see org.springframework.beans.factory.DisposableBean @@ -253,7 +254,18 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable { @Nullable private Method determineDestroyMethod(String name) { try { - return findDestroyMethod(name); + Class beanClass = this.bean.getClass(); + Method destroyMethod = findDestroyMethod(beanClass, name); + if (destroyMethod != null) { + return destroyMethod; + } + for (Class beanInterface : beanClass.getInterfaces()) { + destroyMethod = findDestroyMethod(beanInterface, name); + if (destroyMethod != null) { + return destroyMethod; + } + } + return null; } catch (IllegalArgumentException ex) { throw new BeanDefinitionValidationException("Could not find unique destroy method on bean with name '" + @@ -262,10 +274,10 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable { } @Nullable - private Method findDestroyMethod(String name) { + private Method findDestroyMethod(Class clazz, String name) { return (this.nonPublicAccessAllowed ? - BeanUtils.findMethodWithMinimalParameters(this.bean.getClass(), name) : - BeanUtils.findMethodWithMinimalParameters(this.bean.getClass().getMethods(), name)); + BeanUtils.findMethodWithMinimalParameters(clazz, name) : + BeanUtils.findMethodWithMinimalParameters(clazz.getMethods(), name)); } /**