本文主要研究一下DisposableBeanAdapter
spring-beans/src/main/java/org/springframework/beans/factory/DisposableBean.java
public interface DisposableBean {
/**
* Invoked by the containing {@code BeanFactory} on destruction of a bean.
* @throws Exception in case of shutdown errors. Exceptions will get logged
* but not rethrown to allow other beans to release their resources as well.
*/
void destroy() throws Exception;
}
DisposableBean定义了destroy方法
spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java
class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
private static final String CLOSE_METHOD_NAME = "close";
private static final String SHUTDOWN_METHOD_NAME = "shutdown";
private static final Log logger = LogFactory.getLog(DisposableBeanAdapter.class);
private final Object bean;
private final String beanName;
private final boolean invokeDisposableBean;
private final boolean nonPublicAccessAllowed;
@Nullable
private final AccessControlContext acc;
@Nullable
private String destroyMethodName;
@Nullable
private transient Method destroyMethod;
@Nullable
private final List beanPostProcessors;
@Override
public void run() {
destroy();
}
@Override
public void destroy() {
if (!CollectionUtils.isEmpty(this.beanPostProcessors)) {
for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {
processor.postProcessBeforeDestruction(this.bean, this.beanName);
}
}
if (this.invokeDisposableBean) {
if (logger.isTraceEnabled()) {
logger.trace("Invoking destroy() on bean with name '" + this.beanName + "'");
}
try {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedExceptionAction
DisposableBeanAdapter实现了DisposableBean、Runnable接口,其run方法执行的是destroy方法;其destroy方法会遍历DestructionAwareBeanPostProcessor挨个执行postProcessBeforeDestruction方法,对于invokeDisposableBean的则执行其destroy方法,对于destroyMethod不为null或者destroyMethodName不为null的则通过invokeCustomDestroyMethod执行
它提供了hasDestroyMethod方法用于判断某个bean是否有destroy方法,如果是DisposableBean或者AutoCloseable类型则直接返回true,否则通过inferDestroyMethodIfNecessary方法判断,它目前会把public的无参的close或者shutdown方法(不论是自己定义的还是继承而来的)作为destroyMethod
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
/**
* Add the given bean to the list of disposable beans in this factory,
* registering its DisposableBean interface and/or the given destroy method
* to be called on factory shutdown (if applicable). Only applies to singletons.
* @param beanName the name of the bean
* @param bean the bean instance
* @param mbd the bean definition for the bean
* @see RootBeanDefinition#isSingleton
* @see RootBeanDefinition#getDependsOn
* @see #registerDisposableBean
* @see #registerDependentBean
*/
protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {
AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null);
if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {
if (mbd.isSingleton()) {
// Register a DisposableBean implementation that performs all destruction
// work for the given bean: DestructionAwareBeanPostProcessors,
// DisposableBean interface, custom destroy method.
registerDisposableBean(beanName, new DisposableBeanAdapter(
bean, beanName, mbd, getBeanPostProcessorCache().destructionAware, acc));
}
else {
// A bean with a custom scope...
Scope scope = this.scopes.get(mbd.getScope());
if (scope == null) {
throw new IllegalStateException("No Scope registered for scope name '" + mbd.getScope() + "'");
}
scope.registerDestructionCallback(beanName, new DisposableBeanAdapter(
bean, beanName, mbd, getBeanPostProcessorCache().destructionAware, acc));
}
}
}
protected boolean requiresDestruction(Object bean, RootBeanDefinition mbd) {
return (bean.getClass() != NullBean.class && (DisposableBeanAdapter.hasDestroyMethod(bean, mbd) ||
(hasDestructionAwareBeanPostProcessors() && DisposableBeanAdapter.hasApplicableProcessors(
bean, getBeanPostProcessorCache().destructionAware))));
}
AbstractBeanFactory的registerDisposableBeanIfNecessary方法会通过requiresDestruction判断是否需要销毁,是的话会执行registerDisposableBean或者registerDestructionCallback,这里通过DisposableBeanAdapter进行了包装
DisposableBeanAdapter实现了DisposableBean、Runnable接口,它主要是执行DestructionAwareBeanPostProcessor的postProcessBeforeDestruction方法,对于invokeDisposableBean的则执行其destroy方法,对于destroyMethod不为null或者destroyMethodName不为null的则通过invokeCustomDestroyMethod执行。
DisposableBeanAdapter提供了hasDestroyMethod方法用于判断某个bean是否有destroy方法,如果是DisposableBean或者AutoCloseable类型则直接返回true,否则通过inferDestroyMethodIfNecessary方法判断,它目前会把public的无参的close或者shutdown方法(不论是自己定义的还是继承而来的)作为destroyMethod
值得注意的是自动推断的前提是beanDefinition.getDestroyMethodName()为AbstractBeanDefinition.INFER_METHOD或者是destroyMethodName为null但是bean是AutoCloseable类型,而且推断也只是找close、shutdown方法,如果没有实现DisposableBean接口,但是定义了destory方法,不会被认为是destroyMethod;@Bean注解的destroyMethod默认就是AbstractBeanDefinition.INFER_METHOD,如果是通过GenericApplicationContext.registerBean注册的,则默认destroyMethodName为空,需要自己设置,如果是通过registerBeanDefinition方法的,需要自己保证destroyMethodName为想要执行的销毁方法。