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
This commit is contained in:
Sébastien Deleuze 2023-03-30 15:28:50 +02:00
parent 3a36d51473
commit c5f0f7bb11
1 changed files with 16 additions and 4 deletions

View File

@ -49,6 +49,7 @@ import org.springframework.util.StringUtils;
* @author Costin Leau * @author Costin Leau
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Sam Brannen * @author Sam Brannen
* @author Sebastien Deleuze
* @since 2.0 * @since 2.0
* @see AbstractBeanFactory * @see AbstractBeanFactory
* @see org.springframework.beans.factory.DisposableBean * @see org.springframework.beans.factory.DisposableBean
@ -253,7 +254,18 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
@Nullable @Nullable
private Method determineDestroyMethod(String name) { private Method determineDestroyMethod(String name) {
try { 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) { catch (IllegalArgumentException ex) {
throw new BeanDefinitionValidationException("Could not find unique destroy method on bean with name '" + throw new BeanDefinitionValidationException("Could not find unique destroy method on bean with name '" +
@ -262,10 +274,10 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
} }
@Nullable @Nullable
private Method findDestroyMethod(String name) { private Method findDestroyMethod(Class<?> clazz, String name) {
return (this.nonPublicAccessAllowed ? return (this.nonPublicAccessAllowed ?
BeanUtils.findMethodWithMinimalParameters(this.bean.getClass(), name) : BeanUtils.findMethodWithMinimalParameters(clazz, name) :
BeanUtils.findMethodWithMinimalParameters(this.bean.getClass().getMethods(), name)); BeanUtils.findMethodWithMinimalParameters(clazz.getMethods(), name));
} }
/** /**