一、Spring 生命周期回调机制可选方式

Spring官方文档注明从Spring 2.5开始,您可以使用三种方式来控制Bean生命周期行为:

  • InitializingBean和DisposableBean回调接口

  • 自定义init()destroy()方法

  • @PostConstruct 和 @PreDestroy 注解。

您可以组合以上这些机制来控制给定的 bean。

注:如果为 bean 配置了多个生命周期机制,并且每个机制配置了不同的方法名称,则每个配置的方法都按照下面的顺序执行。但是,如果配置了相同的方法名称 - 对于 example,init()用于初始化方法 - 对于多个这些生命周期机制,该方法将执行一次,如前一节中所述。(--翻译自原文)

二、各种回调机制方式执行顺序

为同一bean配置的多个生命周期机制,使用不同的初始化方法,调用顺序如下:

1、用@PostConstruct注解的方法

2、由InitializingBean回调接口定义的afterPropertiesSet()方法

3、自定义配置的init()方法(xml中的init-method或者@BeaninitMethod属性)

销毁方法调用顺序相同:

1、用@PreDestroy注释的方法

2、由DisposableBean回调接口定义的destroy()方法

3、自定义配置的destroy()方法(xml中的destroy-method或者@BeandestroyMethod属性)

三、代码测试

采用xml配置启动Spring应用(Spring版本为5.0.5.RELEASE):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--开启识别注解--><context:annotation-config/><bean id="x" class="com.dxc.opentalk.springtest.service.X" init-method="customInit"destroy-method="customDestroy"></bean><bean id="y" class="com.dxc.opentalk.springtest.service.Y"></bean>
</beans>

测试类X:

package com.dxc.opentalk.springtest.service;import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;/*** @author dxc* @since 2020-02-10*/
public class X implements InitializingBean, DisposableBean {@Autowiredprivate Y y;private String s;public X() {System.out.println("Construct X");}/**三种初始化回调方法*/public void customInit() {System.out.println("init-method exec...");}public void afterPropertiesSet() throws Exception {System.out.println("InitializingBean exec...");}@PostConstructpublic void annotationInit() {System.out.println("@PostConstruct exec...");}/**三种销毁回调方法*/public void destroy() throws Exception {System.out.println("DisposableBean exec...");}public void customDestroy() {System.out.println("destroy-method exec...");}@PreDestroypublic void annotationDestroy() {System.out.println("@PreDestroy exec...");}}

启动类:

package com.dxc.opentalk.springtest;import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author dxc* @since 2020-02-29*/
public class XmlBootStrap {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = newClassPathXmlApplicationContext("applicationContext.xml");//关闭上下文,测试bean销毁回调方法applicationContext.close();}
}

程序运行结果:

Construct X
Construct Y
@PostConstruct exec...
InitializingBean exec...
init-method exec...
@PreDestroy exec...
DisposableBean exec...
destroy-method exec...

可以看出,初始化回调方法以及销毁回调方法与上述的顺序一致。另外,需要说明的是如果这三种方式中的两个或者三个用到一个方法上,那么这个方法只会被调用一次。测试如下:

修改xml配置:

    <bean id="x" class="com.dxc.opentalk.springtest.service.X" init-method="afterPropertiesSet"destroy-method="destroy"></bean>

让三种方式都配置到一个方法上:

package com.dxc.opentalk.springtest.service;import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;/*** @author dxc* @since 2020-02-10*/
public class X implements InitializingBean, DisposableBean {@Autowiredprivate Y y;private String s;public X() {System.out.println("Construct X");}public void customInit() {System.out.println("init-method exec...");}@PostConstructpublic void afterPropertiesSet() throws Exception {System.out.println("InitializingBean exec...");}public void annotationInit() {System.out.println("@PostConstruct exec...");}@PreDestroypublic void destroy() throws Exception {System.out.println("DisposableBean exec...");}public void customDestroy() {System.out.println("destroy-method exec...");}public void annotationDestroy() {System.out.println("@PreDestroy exec...");}}

程序运行结果如下:

Construct X
Construct Y
InitializingBean exec...
DisposableBean exec...

可见,这种情况下初始化回调方法和销毁回调方法都只执行了一次。

Spring 生命周期回调机制相关推荐

  1. Spring系列之生命周期回调

    生命周期回调方法 Spring在容器初始化bean之后(完成依赖注入后)和销毁前都提供了回调的方法,我们称之为生命周期的回调方法.Spring中提供了三种方式来完成生命周期的回调. 官网解释 直接看S ...

  2. spring生命周期七个过程_Spring杂文(三)Spring循环引用

    众所周知spring在默认单例的情况下是支持循环引用的 Appconfig.java类的代码 @Configurable @ComponentScan("com.sadow") p ...

  3. Spring生命周期Bean初始化过程详解

    Spring生命周期Bean初始化过程详解 Spring 容器初始化 Spring Bean初始化 BeanFactory和FactoryBean 源码分析 Bean的实例化 preInstantia ...

  4. Cocos生命周期回调

    Cocos Creator 为组件脚本提供了生命周期的回调函数.用户只要定义特定的回调函数,Creator 就会在特定的时期自动执行相关脚本,用户不需要手工调用它们. 目前提供给用户的生命周期回调函数 ...

  5. Java持久性API(JPA)第7讲——实体生命周期及生命周期回调方法

    源贴地址:http://blog.csdn.net/javaeeteacher/archive/2007/06/25/1665345.aspx 目标与主要内容: u        掌握实体的生命周期: ...

  6. 【Java从0到架构师】Spring - 生命周期、代理

    生命周期.代理 bean 的生命周期 代理 业务层的一些问题 静态代理 (Static Proxy) 动态代理 (Dynamic Proxy) JDK 动态代理 - Proxy.newProxyIns ...

  7. 老司机带你从源码开始撸Spring生命周期!!!

    导读 Spring在Java Web方面有着举足轻重的地位,spring的源码设计更是被很多开发者所惊叹,巧妙的设计,精细的构思,都注定他的地位.今天陈某大言不惭的带你来从源码角度解析Spring的生 ...

  8. Spring生命周期详解

    导读 Spring中Bean的生命周期从容器的启动到停止,涉及到的源码主要是在org.springframework.context.support.AbstractApplicationContex ...

  9. 微信小程序学习6:页面生命周期回调函数

    微信小程序学习6:页面生命周期回调函数 文章目录 微信小程序学习6:页面生命周期回调函数 1 生命周期图像 2 生命周期回调函数触发条件 onLoad(Object query) onShow() o ...

最新文章

  1. python内存管理机制_python内存管理机制
  2. 【LeetCode】剑指 Offer 32 - II. 从上到下打印二叉树 II
  3. maven学习(4)
  4. html条纹填充色,HTML5/Canvas 上传图片的彩色斑马条纹遮罩效果
  5. jQuery匹配各种条件的选择器用法
  6. C语言实训 --- 仓库管理系统(原代码)
  7. 微信公众号一次群发多个推文
  8. win10计算器_好用的Win10系统计算器,终于可以在移动端设备上面来用了
  9. java中poi搜索工程_POI搜索简介
  10. 解决ie浏览器兼容ES6语法问题
  11. Tomcat配置HTTP协议转HTTPS协议
  12. 如何把老旧笔记本变成一部 Chromebook
  13. esp8266解析php,ESP8266 Bootloader开源代码解析之rboot(一)
  14. idw matlab 程序_idw插值算法的C#和Matlab简单实现
  15. C语言显示无法添加两个指针,【C语言】两个指针(地址)相减
  16. 啥叫一个好售前​顾问
  17. 达内python怎么样_在达内学Python怎么样?我能学会吗?
  18. bga封装扇出过孔,用Allegro软件如何实现BGA封装的扇出?
  19. 正大国际期货:你身边有朋友或者亲人做期货挣钱的没有?
  20. 基于Java Web的汽车租赁系统的设计与实现

热门文章

  1. 【程序员觉醒】提高效率,增加输出
  2. Photoshop基础操作
  3. 6572KK下栏状态栏增加来电闪烁开关
  4. 全方位助力摄影师后期 Nik Collection 5
  5. ripro9.1(9.2)最新修正升级完美版,无加密无后门无需扩展
  6. 中国app开发公司前十名的共同特点
  7. fiddler4提取荣耀Pro机顶盒请求链接
  8. immutable详解
  9. 相对路径和绝对路径的区别
  10. python代码图片头像_用 Python 把朋友头像变表情包? Easy!