springBean生命周期

在整个生命周期中,修改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的工作,初始化方法是

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

invokeInitMethods方法如下

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

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

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

再来看DisposableBeanAdapter的destroy做了什么

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

来看个小demo来验明正身

  • 定义Spring bean TestBeanLifeCycle

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

  • 在applicationContext.xml中声明bean

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

  • 以Junit方式启动容器

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

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

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

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

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

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

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

原文地址:https://www.cnblogs.com/joke0406/p/9488334.html