以“冬奥之光,多彩冰灯”为主题的第四十一届全国专业冰雕比赛在冰城哈尔滨市进入第二天,60名冰雕高手在哈尔滨冰灯艺术游园会园区展开激烈的竞技比拼。

冰雕艺术

1. 概述

Bean的销毁是Bean的生命周期中最后一步,比如在Tomcat等容器关闭的时候会调用Bean的销毁方法,下面逐步分析。

2. 源码分析

在bean创建完成后,就会对这个bean注册一个销毁的Adapter对象,

	protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException { ...... if (instanceWrapper == null) {
//创建对象实例
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
Object bean = instanceWrapper.getWrappedInstance();
Class<?> beanType = instanceWrapper.getWrappedClass();
if (beanType != NullBean.class) {
mbd.resolvedTargetType = beanType;
} ...... try {
// 属性赋值
populateBean(beanName, mbd, instanceWrapper);
// 初始化bean
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
catch (Throwable ex) {
if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
throw (BeanCreationException) ex;
}
else {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
}
} ...... // Register bean as disposable.
try {
// 注册销毁的bean
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
} return exposedObject;
}

registerDisposableBeanIfNecessary方法中disposableBeans集合负责收集需要销毁的bean

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.
// 注册销毁bean的DisposableBeanAdapter对象
registerDisposableBean(beanName,
new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
}
......
public void registerDisposableBean(String beanName, DisposableBean bean) {
synchronized (this.disposableBeans) {
this.disposableBeans.put(beanName, bean);
}
}

DisposableBeanAdapter 对象就是负责 bean 销毁的类,这个类中收集 bean是否实现了 DisposableBean 接口

class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable

是否配置 destroy-method 属性,过滤了 DestructionAwareBeanPostProcessor 类型的接口,如下图所示:

public DisposableBeanAdapter(Object bean, String beanName, RootBeanDefinition beanDefinition,
List<BeanPostProcessor> postProcessors, @Nullable AccessControlContext acc) { ...... this.beanPostProcessors = filterPostProcessors(postProcessors, bean);
}
private List<DestructionAwareBeanPostProcessor> filterPostProcessors(List<BeanPostProcessor> processors, Object bean) {
List<DestructionAwareBeanPostProcessor> filteredPostProcessors = null;
if (!CollectionUtils.isEmpty(processors)) {
filteredPostProcessors = new ArrayList<>(processors.size());
for (BeanPostProcessor processor : processors) {
if (processor instanceof DestructionAwareBeanPostProcessor) {
DestructionAwareBeanPostProcessor dabpp = (DestructionAwareBeanPostProcessor) processor;
if (dabpp.requiresDestruction(bean)) {
filteredPostProcessors.add(dabpp);
}
}
}
}
return filteredPostProcessors;

然后 bean 是在什么时候被销毁呢,在 tomcat 关闭的时候就会调用到 servlet 中的销毁方法,具体是通过类ContextLoaderListener.java 中的contextDestroyed 方法,通过 closeWebApplicationContext 方法一直往下找此为 servlet 规范的使用,一路往下调用。

















最终会进入DisposableBeanAdapter类中的destroy,方法该方法就会根据前面的收集进行调用。

public void destroy() {
// 处理@PreDestroy注解的beanpostProcessor实现类: InitDestroyAnnotationBeanPostProcessor
if (!CollectionUtils.isEmpty(this.beanPostProcessors)) {
for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {
processor.postProcessBeforeDestruction(this.bean, this.beanName);
}
}
// 处理实现DisposableBean接口的bean的销毁
if (this.invokeDisposableBean) {
if (logger.isTraceEnabled()) {
logger.trace("Invoking destroy() on bean with name '" + this.beanName + "'");
}
try {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
((DisposableBean) this.bean).destroy();
return null;
}, this.acc);
}
else {
((DisposableBean) this.bean).destroy();
}
}
catch (Throwable ex) {
String msg = "Invocation of destroy method failed on bean with name '" + this.beanName + "'";
if (logger.isDebugEnabled()) {
logger.warn(msg, ex);
}
else {
logger.warn(msg + ": " + ex);
}
}
} // 处理在配置文件中的bean配置了destroy-method的bean的销毁
if (this.destroyMethod != null) {
invokeCustomDestroyMethod(this.destroyMethod);
}
else if (this.destroyMethodName != null) {
Method methodToInvoke = determineDestroyMethod(this.destroyMethodName);
if (methodToInvoke != null) {
invokeCustomDestroyMethod(ClassUtils.getInterfaceMethodIfPossible(methodToInvoke));
}
}
}

销毁bean的顺序是如下:

1)判断是否需要处理@PreDestroy注解的bean,如果需要,则通过beanpostProcessor实现类 InitDestroyAnnotationBeanPostProcessor处理;

2)判断是否需要处理实现DisposableBean接口的bean的销毁;

3)判断是否需要处理配置文件中的bean配置了destroy-methodbean的销毁。

3. 案例演示

定义Bean,同时加入销毁对应的三种方法;

/**
* @Author: wzj
* @Date: 2021/7/2 11:32
* @Desc:
*/
public class Wzj implements DisposableBean {
public static Wzj factoryMethod() {
CQ cq = new CQ();
SC sc = new SC();
return new Wzj(sc, cq);
} @PreDestroy
public void close() {
System.out.println("通过 @PreDestroy:销毁实例wzj");
} public void destroyMethod() {
System.out.println("通过配置文件配置destroy-method:销毁实例wzj");
} @Override
public void destroy() {
System.out.println("通过DisposableBean接口:销毁实例wzj");
}

配置文件如下:

	<bean id="wzj"  class="com.wzj.bean.Wzj" factory-method="factoryMethod" destroy-method="destroyMethod"/>

测试类:

/**
* @Author: wzj
* @Date: 2021/3/30 15:08
* @Desc: 测试类
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class TestSpring { @Autowired
private ApplicationContext applicationContext; @Test
public void testDestroy() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Wzj wzj = (Wzj)applicationContext.getBean("wzj");
applicationContext.getBeanFactory().destroyBean("wzj"); }

结果:

有人可能会问,为何Bean可以多次销毁,其实Bean的销毁并不是真正意义上的销毁Bean,而是在销毁前执行销毁方法,可能包含关闭数据库连接、关闭网络请求等逻辑操作,而后真正的销毁是由Spring容器执行关闭,其内部Bean也就自然而然消失了,Bean销毁是发生在Spring容器关闭过程中的。

最新文章

  1. Android Studio踩坑记
  2. 用UIImageView作出动画效果
  3. Log4Net学习【二】
  4. Slony-I 文摘
  5. CentOS 6.4 下搭建 MongoDB 2.4.9 环境
  6. Oracle RAC中的一台机器重启以后无法接入集群
  7. java核心技术记录之java术语
  8. 运用CMD命令关于快速获取文件夹名称和快速建立文件夹
  9. 获取webshell的十种方法
  10. 表达式树 Expression
  11. Java 写三角形 空心三角形 菱形 空心菱形
  12. 关于&#39;selffilter&#39; is not a registered tag library. Must be one of:
  13. 了解golang的可变参数(... parameters),这一篇就够了
  14. python3字符串与文本处理
  15. redis主从复制踩到的那些坑
  16. Java SSM框架之MyBatis3(五)MyBatis之ResultMap详解
  17. 003.KVM虚拟机部署-CentOS6.8
  18. HDU 4778 Gems Fight! (2013杭州赛区1009题,状态压缩,博弈)
  19. 解决Coursera平台上Andrew.Ng的机器学习课程无法正常提交编程作业的问题
  20. Ubuntu18.04安装Python3.6.8

热门文章

  1. CSS之 sass、less、stylus 预处理器的使用方式
  2. Java项目开发中实现分页的三种方式一篇包会
  3. [源码解析] 模型并行分布式训练 Megatron (3) ---模型并行实现
  4. 通过json动态创建控制器
  5. c语言中数组的定义和java中数组定义的一些区别
  6. Base64补充
  7. Td 内容不换行,超过部分自动截断,用...表示
  8. Nodejs ORM框架Sequelize(模型,关联表,事务,循环,及常见问题)
  9. shell脚本命令(sotr/unip/tr/cut/eval)与正则表达式
  10. linux_16