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:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -303,7 +303,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
|
||||
setInitMethodName(otherAbd.getInitMethodName());
|
||||
setEnforceInitMethod(otherAbd.isEnforceInitMethod());
|
||||
}
|
||||
if (StringUtils.hasLength(otherAbd.getDestroyMethodName())) {
|
||||
if (otherAbd.getDestroyMethodName() != null) {
|
||||
setDestroyMethodName(otherAbd.getDestroyMethodName());
|
||||
setEnforceDestroyMethod(otherAbd.isEnforceDestroyMethod());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -38,6 +38,7 @@ import org.springframework.beans.factory.config.DestructionAwareBeanPostProcesso
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Adapter that implements the {@link DisposableBean} and {@link Runnable} interfaces
|
||||
@@ -50,6 +51,7 @@ import org.springframework.util.ReflectionUtils;
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Costin Leau
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0
|
||||
* @see AbstractBeanFactory
|
||||
* @see org.springframework.beans.factory.DisposableBean
|
||||
@@ -186,8 +188,9 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
|
||||
* interfaces, reflectively calling the "close" method on implementing beans as well.
|
||||
*/
|
||||
private String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {
|
||||
if (AbstractBeanDefinition.INFER_METHOD.equals(beanDefinition.getDestroyMethodName()) ||
|
||||
(beanDefinition.getDestroyMethodName() == null && closeableInterface.isInstance(bean))) {
|
||||
String destroyMethodName = beanDefinition.getDestroyMethodName();
|
||||
if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName) ||
|
||||
(destroyMethodName == null && closeableInterface.isInstance(bean))) {
|
||||
// Only perform destroy method inference or Closeable detection
|
||||
// in case of the bean not explicitly implementing DisposableBean
|
||||
if (!(bean instanceof DisposableBean)) {
|
||||
@@ -205,7 +208,7 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return beanDefinition.getDestroyMethodName();
|
||||
return (StringUtils.hasLength(destroyMethodName) ? destroyMethodName : null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -399,7 +402,7 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
|
||||
if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName)) {
|
||||
return ClassUtils.hasMethod(bean.getClass(), CLOSE_METHOD_NAME);
|
||||
}
|
||||
return (destroyMethodName != null);
|
||||
return StringUtils.hasLength(destroyMethodName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -641,9 +641,7 @@ public class BeanDefinitionParserDelegate {
|
||||
|
||||
if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
|
||||
String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE);
|
||||
if (!"".equals(destroyMethodName)) {
|
||||
bd.setDestroyMethodName(destroyMethodName);
|
||||
}
|
||||
bd.setDestroyMethodName(destroyMethodName);
|
||||
}
|
||||
else {
|
||||
if (this.defaults.getDestroyMethod() != null) {
|
||||
|
||||
Reference in New Issue
Block a user