Customize destruction callback for AutoCloseable beans

Previously, a Bean implementing `AutoCloseable` (or `Closeable`) was
always destroyed regardless of its bean definition. In particular, the
documented way of disabling the destruction callback via an empty String
did not work.

AutoCloseable beans are now treated pretty much as any other bean: we
still use the presence of the interface to optimize the check of a
destroy method and we only auto-discover the method name to invoke if
the inferred mode is enabled.

Issue: SPR-13022
This commit is contained in:
Stephane Nicoll
2015-05-15 11:22:29 +02:00
parent 271804f105
commit 0ed9ca097b
8 changed files with 32 additions and 15 deletions

View File

@@ -236,7 +236,7 @@ class ConfigurationClassBeanDefinitionReader {
}
String destroyMethodName = bean.getString("destroyMethod");
if (StringUtils.hasText(destroyMethodName)) {
if (destroyMethodName != null) {
beanDef.setDestroyMethodName(destroyMethodName);
}

View File

@@ -165,8 +165,8 @@ class ScriptBeanDefinitionParser extends AbstractBeanDefinitionParser {
bd.setInitMethodName(beanDefinitionDefaults.getInitMethodName());
}
String destroyMethod = element.getAttribute(DESTROY_METHOD_ATTRIBUTE);
if (StringUtils.hasLength(destroyMethod)) {
if (element.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
String destroyMethod = element.getAttribute(DESTROY_METHOD_ATTRIBUTE);
bd.setDestroyMethodName(destroyMethod);
}
else if (beanDefinitionDefaults.getDestroyMethodName() != null) {

View File

@@ -505,7 +505,7 @@ public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProces
Signature signature = new Signature(abd.getInitMethodName(), Type.VOID_TYPE, new Type[0]);
maker.add(signature, new Type[0]);
}
if (abd.getDestroyMethodName() != null) {
if (StringUtils.hasText(abd.getDestroyMethodName())) {
Signature signature = new Signature(abd.getDestroyMethodName(), Type.VOID_TYPE, new Type[0]);
maker.add(signature, new Type[0]);
}