Spring bean生命周期不可不知的接口 - Spring源码解析

Spring bean生命周期不可不知的接口

张涛 Spring源码解析;)

Spring源码解析

微信号 SpringAutowired

功能介绍 专注服务端技术分享,spring springMVC springBoot 以及其它Java开源框架源码解析,包括基础,进阶技能,学习,研究,交流。

2017-03-04

上回说到 Spring bean生命周期中的InitializingBean接口,我们了解到这只是spring bean生命周期中修改bean的诸多方式之一,其实在整个生命周期中,修改bean的方式大致有如下几种:

  1. 实现 InitializingBean接口 的 afterPropertiesSet() 
  2. 实现 DisposableBean接口的 destroy()
  3. 给方法加@PostConstruct和@PreDestroy注解,需要说明一点,这两个注解是JSR250标准中的,而Spring中InitDestroyAnnotationBeanPostProcessor类实现了这个标准
  4. 以xml的方式定义bean时,指定init method和destroy method

    <bean id="testBeanLifeCycle" class="org.springframework.samples.mvc.beanPostProcessor.TestBeanLiftCycle"
         init-method="xmlInit" destroy-method="xmlDestroy">
       <property name="message" value="Hello World!"/</bean>

在众多的lifecycle机制中,假设一个bean使用了上边所有的方式,那么它们的执行顺序是这样的:

  • Methods annotated with @PostConstruct
  • afterPropertiesSet() as defined by the InitializingBean callback interface
  • A custom configured init() method
  • Methods annotated with @PreDestroy
  • destroy() as defined by the DisposableBean callback interface
  • A custom configured destroy() method

我们在代码中来看

在Spring中,bean的生命周期管理都是由beanFactory来管理,上一篇我们有提到,实现了beanFactory的类AbstractAutowiredCapableBeanFactory完成了bean的init和destroy的工作,初始化方法是

invokeInitMethods方法如下

接下来看看destroy逻辑,在AbstractAutowiredCapableBeanFactory中我们找到destroy方法

再来看DisposableBeanAdapter的destroy做了什么

来看个小demo来验明正身

  • 定义Spring bean TestBeanLifeCycle

  • 在applicationContext.xml中声明bean

  • 以Junit方式启动容器

  • 然后我们会看到下面的输出:

说了这么多机制,那么我们在使用的时候要注意什么

推荐使用@PostConstruct和@PreDestroy,因为这来自JSR250,对Spring框架0耦合。

除此之外,Spring还提供了LifeCycle接口和LifeCycleProcessor接口,实现这些接口能让bean的lifecycle关联到Container的lifeCycle,当容器有启动,停止,刷新等动作时,会回调到bean。

如果说所有这些Spring提供的机制还是不能满足你的需求,那么总有一款适合你——BeanPostProcessor接口,事实上在Spring架构内部,众多的beanPostProcessor实现,能完成各种接口的回调,或者自定义方法的触发。关于更多BeanPostProcessor,且听下回分解。

    • *

欢迎关注,期待与您的交流,让我们携手在通往牛逼的小路上徐徐前行。

张涛

赞赏

长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。


Original url: Access
Created at: 2019-04-15 11:17:05
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论