当前位置: 首页 > news >正文

【Spring专题】Spring之Bean生命周期源码解析——阶段四(Bean销毁)(拓展,了解就好)

目录

  • 前言
    • 阅读建议
  • 课程内容
    • 一、Bean什么时候销毁
    • 二、实现自定义的Bean销毁逻辑
      • 2.1 实现DisposableBean或者AutoCloseable接口
      • 2.2 使用@PreDestroy注解
      • 2.3 其他方式(手动指定销毁方法名字)
    • 三、注册销毁Bean过程及方法详解
      • 3.1 AbstractBeanFactory#requiresDestruction:需要销毁吗
      • 3.2 DisposableBeanAdapter.hasDestroyMethod:是否有销毁方法
      • 3.3 DisposableBeanAdapter#inferDestroyMethodIfNecessary:推断销毁方法
      • 3.4 AbstractBeanFactory#hasDestructionAwareBeanPostProcessors:是否有感知销毁Bean后置处理器
      • 3.5 DisposableBeanAdapter.hasApplicableProcessors:是否有应用于当前Bean的销毁感知Bean后置处理器
      • 3.6 DefaultSingletonBeanRegistry#registerDisposableBean:注册需要销毁的bean
      • 3.7 注册销毁Bean过程总结
    • 四、注册销毁Bean逻辑流程图
    • 五、概念回顾
  • 学习总结

前言

我们在这里讲的是Bean的销毁过程。也许,不少朋友说到Bean的销毁,可能会想到垃圾回收的东西。虽然都是在做生命周期的最后一部分,但其实这俩不是同一回事。垃圾回收是JVM级别的东西,这里说的Bean销毁是Spring的东西,所以当然不是一回事。

阅读建议

本节课的内容,将会以下面这段代码为入口讲解:

		// 注册Bean的销毁接口try {registerDisposableBeanIfNecessary(beanName, bean, mbd);}catch (BeanDefinitionValidationException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);}return exposedObject;

这段代码,其实是在Spring实例化里面的AbstractAutowireCapableBeanFactory#doCreateBean()方法里面。而且,通过这个方法名字大家也知道了,这一步仅仅只是注册销毁逻辑而已,并不是真的销毁。只有当一定条件成立的时候,才会去销毁。
registerDisposableBeanIfNecessary具体代码如下:

  /*** 将给定bean添加到此工厂中的一次性bean列表中,注册其DisposableBean接口和/或给定的destroy方法,以便在工厂关闭时调用(如果适用)。只适用于单例。 * 参数: * beanName—bean的名称—bean实例mbd—bean的bean定义 * 参见: * RootBeanDefinition。isSingleton RootBeanDefinition。getDependsOn, registerDisposableBean, 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));}}}

课程内容

一、Bean什么时候销毁

Bean销毁是发生在Spring容器关闭过程中。这时,Spring所有的单例Bean都会被销毁,并且,会执行各自实现了自定义销毁逻辑的Bean的销毁方法。我们在本篇文章要介绍的,就是:如何实现自定义的Bean销毁逻辑
在Spring容器关闭时,可以显示关闭,比如:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = (UserService) context.getBean("userService");
userService.test();// 容器关闭
context.close();

又或者,注册一个关闭钩子:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);// 注册关闭钩子
context.registerShutdownHook();Object newUser = context.getBean("user");
System.out.println(newUser);

【注意:强制杀掉进程(kill pid)是不会调用自定义的Bean销毁逻辑】

Spring关闭容器的过程:

  1. 首先发布ContextClosedEvent事件
  2. 调用lifecycleProcessor的onCloese()方法
  3. 销毁单例Bean
    • 遍历disposableBeans
      • 把每个disposableBean从单例池中移除
      • 调用disposableBean的destroy()
      • 如果这个disposableBean还被其他Bean依赖了,那么也得销毁其他Bean
      • 如果这个disposableBean还包含了inner beans,将这些Bean从单例池中移除掉
    • 清空manualSingletonNames,是一个Set,存的是用户手动注册的单例Bean的beanName
    • 清空allBeanNamesByType,是一个Map,key是bean类型,value是该类型所有的beanName数组
    • 清空singletonBeanNamesByType,和allBeanNamesByType类似,只不过只存了单例Bean

二、实现自定义的Bean销毁逻辑

实现方式有如下几种:

2.1 实现DisposableBean或者AutoCloseable接口

需要自定义销毁的Bean代码示例:(实现自:DisposableBean )

@Component
public class TestDestroyBean implements DisposableBean {public void test() {System.out.println("测试一下销毁方法");}@Overridepublic void destroy() throws Exception {System.out.println("TestDestroyBean------自定义的Bean销毁方法");}
}

或者:(实现自:AutoCloseable )

@Component
public class TestDestroyBean implements AutoCloseable {public void test() {System.out.println("测试一下销毁方法");}@Overridepublic void close() throws Exception {System.out.println("TestDestroyBean------自定义的Bean销毁方法");}
}

2.2 使用@PreDestroy注解

实现方式有如下3种:

@Component
public class TestDestroyBean {public void test() {System.out.println("测试一下销毁方法");}@PreDestroypublic void close() throws Exception {System.out.println("TestDestroyBean------自定义的Bean销毁方法");}
}

2.3 其他方式(手动指定销毁方法名字)

当然,还有其他方式,如:

<bean destroy-method='xxx'>

或者:

@Bean(destroyMethod = "xxx")

又或者就是,在beanDefinition里面直接指定销毁方法:

@Component
public class MyBeanPostProcessor implements MergedBeanDefinitionPostProcessor {@Overridepublic void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {if (beanName.equals("user")) {beanDefinition.setInitMethodName("myInit");beanDefinition.setDestroyMethodName("xxxx");}}
}

上面说的这三种方式,有一个比较特殊的地方,因为是手动指定的,所以可以设置一个比较特殊的值:(inferred)
如果设置了销毁方法名字为这个,并且Bean没有实现DisposableBean,则,在销毁的过程中,会检索bean下面有没有close或者shutdown方法。有,则自动绑定为【用户自定义销毁方法】。

三、注册销毁Bean过程及方法详解

本次销毁过程总过涉及了【3个核心类,6个核心方法】

3.1 AbstractBeanFactory#requiresDestruction:需要销毁吗

方法调用链:从入口:registerDisposableBeanIfNecessary()调用进来
全路径:org.springframework.beans.factory.support.AbstractBeanFactory#requiresDestruction
方法注释:将给定bean添加到此工厂中的一次性bean列表中,注册其DisposableBean接口和/或给定的destroy方法,以便在工厂关闭时调用(如果适用)。只适用于单例。

源码如下:

protected boolean requiresDestruction(Object bean, RootBeanDefinition mbd) {return (bean.getClass() != NullBean.class && (DisposableBeanAdapter.hasDestroyMethod(bean, mbd) || (hasDestructionAwareBeanPostProcessors() && DisposableBeanAdapter.hasApplicableProcessors(bean, getBeanPostProcessorCache().destructionAware))));
}

方法解读:里面的关键源码其实就是分两个步骤。如下:

  1. 是否有指定的销毁方法。DisposableBeanAdapter.hasDestroyMethod(bean, mbd)
  2. 是否有DestructionAwareBeanPostProcessor,能感知销毁的Bean后置处理器(hasDestructionAwareBeanPostProcessors)。有则遍历DisposableBeanAdapter.hasApplicableProcessors(bean, getBeanPostProcessorCache().destructionAware)

3.2 DisposableBeanAdapter.hasDestroyMethod:是否有销毁方法

方法调用链:由3.1中的requiresDestruction()调用过来
全路径:org.springframework.beans.factory.support.DisposableBeanAdapter#hasDestroyMethod
方法注释:检查给定bean是否有任何要调用的销毁方法。

源码如下:

public static boolean hasDestroyMethod(Object bean, RootBeanDefinition beanDefinition) {return (bean instanceof DisposableBean || inferDestroyMethodIfNecessary(bean, beanDefinition) != null);
}

而里面的内容其实也很简单,步骤如下:

  1. 当前Bean是否实现了DisposableBean接口
  2. 没有,则调用inferDestroyMethodIfNecessary推断销毁方法(后面讲)

3.3 DisposableBeanAdapter#inferDestroyMethodIfNecessary:推断销毁方法

方法调用链:由3.2中的hasDestroyMethod()调用过来
全路径:org.springframework.beans.factory.support.DisposableBeanAdapter#inferDestroyMethodIfNecessary
方法注释:
如果给定beanDefinition的"destroyMethodName"属性的当前值是AbstractBeanDefinition。然后尝试推断一个销毁方法。候选方法目前仅限于名为“close”或“shutdown”的公共无参数方法(无论是在本地声明还是继承)。如果没有找到这样的方法,则将给定BeanDefinition的“destroyMethodName”更新为null,否则将设置为推断方法的名称。该常量作为@Bean#destroyMethod属性的默认值,该常量的值也可以在XML中或属性中使用。还处理java.io.Closeable和AutoCloseable接口,并在实现bean时反射地调用“close”方法。

源码如下:

	@Nullableprivate static String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {String destroyMethodName = beanDefinition.resolvedDestroyMethodName;if (destroyMethodName == null) {destroyMethodName = beanDefinition.getDestroyMethodName();boolean autoCloseable = (bean instanceof AutoCloseable);if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName) ||(destroyMethodName == null && autoCloseable)) {// 当销毁方法名字等于"(inferred)",且bean不是DisposableBean实现类destroyMethodName = null;if (!(bean instanceof DisposableBean)) {if (autoCloseable) {destroyMethodName = CLOSE_METHOD_NAME;}else {try {destroyMethodName = bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();}catch (NoSuchMethodException ex) {try {destroyMethodName = bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();}catch (NoSuchMethodException ex2) {// no candidate destroy method found}}}}}beanDefinition.resolvedDestroyMethodName = (destroyMethodName != null ? destroyMethodName : "");}return (StringUtils.hasLength(destroyMethodName) ? destroyMethodName : null);}

方法解读:没啥好解读的了,直接重复贴一边注释就好了。尝试推断一个销毁方法。候选方法目前仅限于名为“close”或“shutdown”的公共无参数方法(无论是在本地声明还是继承)。如果没有找到这样的方法,则将给定BeanDefinition的“destroyMethodName”更新为null,否则将设置为推断方法的名称。该常量作为@Bean#destroyMethod属性的默认值,该常量的值也可以在XML中<bean destroy-method=“”>或属性中使用。还处理java.io.Closeable和AutoCloseable接口,并在实现bean时反射地调用“close”方法。

3.4 AbstractBeanFactory#hasDestructionAwareBeanPostProcessors:是否有感知销毁Bean后置处理器

方法调用链:由3.1中的requiresDestruction()调用过来
全路径:org.springframework.beans.factory.support.AbstractBeanFactory#hasDestructionAwareBeanPostProcessors
方法注释:返回该工厂是否持有一个DestructionAwareBeanPostProcessor,该DestructionAwareBeanPostProcessor将在关闭时应用于单例bean。

源码如下:

	protected boolean hasDestructionAwareBeanPostProcessors() {return !getBeanPostProcessorCache().destructionAware.isEmpty();}

3.5 DisposableBeanAdapter.hasApplicableProcessors:是否有应用于当前Bean的销毁感知Bean后置处理器

方法调用链:由3.1中的requiresDestruction()调用过来
全路径:org.springframework.beans.factory.support.DisposableBeanAdapter#hasApplicableProcessors
方法注释:检查给定bean是否有应用于它的销毁感知后处理器。

源码如下:

	public static boolean hasApplicableProcessors(Object bean, List<DestructionAwareBeanPostProcessor> postProcessors) {if (!CollectionUtils.isEmpty(postProcessors)) {for (DestructionAwareBeanPostProcessor processor : postProcessors) {if (processor.requiresDestruction(bean)) {return true;}}}return false;}

经典的BeanPostProcessor处理了,不说了

3.6 DefaultSingletonBeanRegistry#registerDisposableBean:注册需要销毁的bean

方法调用链:从入口:registerDisposableBeanIfNecessary()调用进来
全路径:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#registerDisposableBean
方法注释:将给定的bean添加到此注册中心的销毁bean列表中。

源码如下:

private final Map<String, Object> disposableBeans = new LinkedHashMap<>();public void registerDisposableBean(String beanName, DisposableBean bean) {synchronized (this.disposableBeans) {this.disposableBeans.put(beanName, bean);}
}

方法解读:所谓的注册,其实就是将当前bean及一些信息,添加到一个缓存map中。等到需要用到的时候,直接遍历map就好

3.7 注册销毁Bean过程总结

整体来说分为2个步骤:

  1. 是单例bean,判断是否需要销毁。判断步骤如下:(不同的Spring版本细节不一样,但是整体是一致的
    • 当前Bean是否实现了DisposableBean接口,是则直接返回true;否则进行【推断销毁方法】流程
    • 推断销毁方法
      • BeanDefinition中是否指定了destroyMethod,且destroyMethod==(inferred)。如果是,则寻找当前bean下是否有close方法或者shutdown方法,是则直接返回销毁方法名称
      • 或者当前Bean是否实现了AutoCloseable接口,是则直接返回销毁方法名称
    • 如果【推断销毁方法】也没有结果,则调用【感知销毁Bean后置处理器】DestructionAwareBeanPostProcessor.requiresDestruction(bean)进行判断
      • ApplicationListenerDetector中直接使得,如果当前bean是ApplicationListener子类需要销毁
      • InitDestroyAnnotationBeanPostProcessor中使得拥有@PreDestroy注解了的方法就是需要销毁
  2. 如果需要销毁,则适配成DisposableBeanAdapter对象,并存入disposableBeans中(一个LinkedHashMap)

四、注册销毁Bean逻辑流程图

在这里插入图片描述

五、概念回顾

这里用到的一个比较重要的后置处理器是InitDestroyAnnotationBeanPostProcessor,它的定义如下:

    /***通用场景术语库beanpostprocessor实现,调用带注释的init和destroy方法。允许一个注释替代Spring的org.springframework.beans.factory.InitializingBean和org.springframework.beans.factory.DisposableBean回调接口。这个后处理器检查的实际注释类型可以通过“initAnnotationType”和“destroyAnnotationType”属性来配置。可以使用任何自定义注释,因为没有必需的注释属性。Init和destroy注释可以应用于任何可见性的方法:public、package-protected、protected或private。可以注释多个这样的方法,但建议分别只注释一个init方法和destroy方法。Spring的org.springframework.context.annotation.CommonAnnotationBeanPostProcessor支持JSR-250开箱即用的javax.annotation.PostConstruct和javax.annotation.PreDestroy注释,就像init annotation和destroy annotation一样。此外,它还支持javax.annotation.Resource注释,用于注释驱动的命名bean注入。自:2.5参见:setInitAnnotationType, setDestroyAnnotationType作者:Juergen hoel以上翻译结果来自有道神经网络翻译(YNMT)· 通用场景逐句对照*/public class InitDestroyAnnotationBeanPostProcessorimplements DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, PriorityOrdered, Serializable {
}

学习总结

  1. 学习了Bean销毁过程
  2. 学习了注册销毁Bean的逻辑
  3. 学习了如何自定义Bean销毁逻辑
http://www.lryc.cn/news/133569.html

相关文章:

  • 配置Docker,漏洞复现
  • 微信小程序 游戏水平评估系统的设计与实现_pzbe0
  • moba登录不进去提示修改问题问题解决方式
  • Unsafe upfileupload
  • 机器人制作开源方案 | 滑板助力器
  • 飞机打方块(二)游戏界面制作
  • 自我理解:精度(precision)和召回(recall)
  • Nginx 使用 HTTPS(准备证书和私钥)
  • Java:集合框架:Set集合、LinkedSet集合、TreeSet集合、哈希值、HashSet的底层原理
  • 自定义Taro的navBar的宽度和高度
  • 用Python编程实现百度自然语言处理接口的对接,助力你开发智能化处理程序
  • 系统架构设计专业技能 · 系统工程与系统性能
  • 初识网络原理(笔记)
  • 嵌入式C语言基本操作方法之经典
  • postgresql \watch实用的使用方法
  • Cocos2d 项目问题记录
  • 系统架构合理性的思考 | 京东云技术团队
  • Amelia预订插件:WordPress企业级预约系统
  • 共享门店模式:线下门店的商家如何利用它增加客户
  • 实现矩阵地图与rviz地图重合
  • 设计模式十九:备忘录模式(Memento Pattern)
  • 【题解】二叉搜索树与双向链表
  • 【真实案例】解决后端接口调用偶尔超时问题
  • 操作符详解(1)
  • <指针进阶>指针数组和数组指针傻傻分不清?
  • 无代码集成飞书连接更多应用
  • 三分钟解决AE缓存预览渲染错误、暂停、卡顿问题
  • 朴实无华的数据增强然后训练一下应用在电网异物检测领域,好像有自己的数据集就能发文了
  • 【使用教程】在Ubuntu下运行CANopen通信PMM伺服电机使用教程(NimServoSDK_V2.0.0)
  • vue3+ts+vite项目页面初始化loading加载效果