Skip to content

Commit fd159ad

Browse files
committed
Custom init/destroy methods get invoked through interface is possible
Closes gh-22939
1 parent dec6d69 commit fd159ad

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ protected void invokeCustomInitMethod(String beanName, final Object bean, RootBe
18601860

18611861
String initMethodName = mbd.getInitMethodName();
18621862
Assert.state(initMethodName != null, "No init method set");
1863-
final Method initMethod = (mbd.isNonPublicAccessAllowed() ?
1863+
Method initMethod = (mbd.isNonPublicAccessAllowed() ?
18641864
BeanUtils.findMethod(bean.getClass(), initMethodName) :
18651865
ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName));
18661866

@@ -1882,15 +1882,16 @@ protected void invokeCustomInitMethod(String beanName, final Object bean, RootBe
18821882
if (logger.isTraceEnabled()) {
18831883
logger.trace("Invoking init method '" + initMethodName + "' on bean with name '" + beanName + "'");
18841884
}
1885+
Method methodToInvoke = ClassUtils.getInterfaceMethodIfPossible(initMethod);
18851886

18861887
if (System.getSecurityManager() != null) {
18871888
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
1888-
ReflectionUtils.makeAccessible(initMethod);
1889+
ReflectionUtils.makeAccessible(methodToInvoke);
18891890
return null;
18901891
});
18911892
try {
18921893
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () ->
1893-
initMethod.invoke(bean), getAccessControlContext());
1894+
methodToInvoke.invoke(bean), getAccessControlContext());
18941895
}
18951896
catch (PrivilegedActionException pae) {
18961897
InvocationTargetException ex = (InvocationTargetException) pae.getException();

spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -112,15 +112,15 @@ public DisposableBeanAdapter(Object bean, String beanName, RootBeanDefinition be
112112
if (destroyMethodName != null && !(this.invokeDisposableBean && "destroy".equals(destroyMethodName)) &&
113113
!beanDefinition.isExternallyManagedDestroyMethod(destroyMethodName)) {
114114
this.destroyMethodName = destroyMethodName;
115-
this.destroyMethod = determineDestroyMethod(destroyMethodName);
116-
if (this.destroyMethod == null) {
115+
Method destroyMethod = determineDestroyMethod(destroyMethodName);
116+
if (destroyMethod == null) {
117117
if (beanDefinition.isEnforceDestroyMethod()) {
118118
throw new BeanDefinitionValidationException("Could not find a destroy method named '" +
119119
destroyMethodName + "' on bean with name '" + beanName + "'");
120120
}
121121
}
122122
else {
123-
Class<?>[] paramTypes = this.destroyMethod.getParameterTypes();
123+
Class<?>[] paramTypes = destroyMethod.getParameterTypes();
124124
if (paramTypes.length > 1) {
125125
throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" +
126126
beanName + "' has more than one parameter - not supported as destroy method");
@@ -129,7 +129,9 @@ else if (paramTypes.length == 1 && boolean.class != paramTypes[0]) {
129129
throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" +
130130
beanName + "' has a non-boolean parameter - not supported as destroy method");
131131
}
132+
destroyMethod = ClassUtils.getInterfaceMethodIfPossible(destroyMethod);
132133
}
134+
this.destroyMethod = destroyMethod;
133135
}
134136
this.beanPostProcessors = filterPostProcessors(postProcessors, bean);
135137
}
@@ -271,9 +273,9 @@ public void destroy() {
271273
invokeCustomDestroyMethod(this.destroyMethod);
272274
}
273275
else if (this.destroyMethodName != null) {
274-
Method methodToCall = determineDestroyMethod(this.destroyMethodName);
275-
if (methodToCall != null) {
276-
invokeCustomDestroyMethod(methodToCall);
276+
Method methodToInvoke = determineDestroyMethod(this.destroyMethodName);
277+
if (methodToInvoke != null) {
278+
invokeCustomDestroyMethod(ClassUtils.getInterfaceMethodIfPossible(methodToInvoke));
277279
}
278280
}
279281
}

0 commit comments

Comments
 (0)