【Spring】Spring之Bean生命周期源码解析(下)

Bean的销毁过程

Bean销毁是发送在Spring容器关闭过程中的。

在Spring容器关闭时,比如:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = (UserService) context.getBean("userService");
userService.test();

// 容器关闭
context.close();

在Bean创建过程中,在最后(初始化之后),有一个步骤会去判断当前创建的Bean是不是DisposableBean:

1、当前Bean是否实现了DisposableBean接口

2、或者,当前Bean是否实现了AutoCloseable接口

3、BeanDefinition中是否指定了destroyMethod

4、调用DestructionAwareBeanPostProcessor.requiresDestruction(bean)进行判断

        1) ApplicationListenerDetector中直接使得ApplicationListener是DisposableBean

        2)InitDestroyAnnotationBeanPostProcessor中使得拥有@PreDestroy注解了的方法就是DisposableBean

5、把符合上述任意一个条件的Bean适配成DisposableBeanAdapter对象,并存入disposableBeans中(一个LinkedHashM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值