七、spring生命周期之初始化和销毁方法
一、通过@Bean指定初始化和销毁方法
在以往的xml中,我们是这样配置的
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init" destroy-method="cleanup"/>
那如果采用注解 的方式该如何配置呢?
首先我们创建一个Car,
public class Car {public Car(){System.out.println("car constructor..."); // 构造方法}public void init(){System.out.println("car ... init..."); // 初始化方法}public void detory(){System.out.println("car ... detory..."); // 销毁方法}}
我们需要通过配置类将这个Car注册进容器中,当然这很简单。
@Configuration
public class MainConfigOfLifeCycle {//@Scope("prototype")@Scope("singleton") // 默认即单例@Bean(initMethod="init",destroyMethod="detory")public Car car(){return new Car();}}
这样就搞定了,在ioc容器启动的时候,将会把Car这个类加载进容器创建其单例对象。
那我们主要是观察其初始化和销毁方法
测试方法:
@Testpublic void test01(){//1、创建ioc容器AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);System.out.println("容器创建完成...");//applicationContext.getBean("car");//关闭容器applicationContext.close();}
打印其输出,
car constructor...
car ... init...
容器创建完成...
cat...destroy...
小结: bean的生命周期:bean创建---初始化----销毁的过程,
容器管理bean的生命周期;
我们可以自定义初始化和销毁方法;
容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
观察上面的结果,也可以发现,在单例模式下,容器启动时则创建单例的car对象,并调用其初始化方法init,当容器要关闭时,执行其销毁方法destroy。
在多例模式下,这里没测,多例模式下,当我们主动获取对象的时候,容器才会创建bean,类似于一种懒加载机制,另外,在多例模式下,我们观察不到销毁方法的执行,因为多例模式下,spring的ioc不再管理我们自定义的销毁方法。
二、实现InitializingBean,DisposableBean定义初始化和销毁方法
InitializingBean
接口只有一个抽象方法:
void afterPropertiesSet() throws Exception;
DisposableBean
接口也只有一个抽象方法
void destroy() throws Exception;
我们写一个普通类继承这两个接口 ,
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;@Component
public class Cat implements InitializingBean,DisposableBean {public Cat(){System.out.println("cat constructor...");}@Overridepublic void destroy() throws Exception {System.out.println("cat...destroy...");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("cat...afterPropertiesSet...");}
}
编写一个测试方法,启动ioc容器,测试它的生命周期
@Testpublic void test01(){//1、创建ioc容器AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);System.out.println("容器创建完成...");//applicationContext.getBean("car");//关闭容器applicationContext.close();}
控制台打印:
cat constructor...
cat...afterPropertiesSet...
容器创建完成...
cat...destroy...
从方法名就能看出,在cat这个对象的的属性都被注入之后,就会调用afterPropertiesSet
这个方法
再容器销毁前,会调用Cat
类的destroy方法。
三、通过@PostConstruct和@PreDestroy
这两个是JSR250规范中的,标注在方法上,也是在对象创建完成属性赋值之后,同样也是在容器销毁之前@PreDestroy标注的方法会被调用
我们也来举例测试一下,
创建一个Dog类
@Component
public class Dog{public Dog(){System.out.println("dog constructor...");}//对象创建并赋值之后调用@PostConstructpublic void init(){System.out.println("Dog....@PostConstruct...");}//容器移除对象之前@PreDestroypublic void detory(){System.out.println("Dog....@PreDestroy...");}}
依旧使用上面的测试方法进行测试
输出结果如下
dog constructor...
Dog....@PostConstruct...
容器创建完成...
Dog....@PreDestroy...
四、 小结
以上我们介绍了三种方法对容器中的对象进行初始化和销毁,这只是spring容器中bean生命周期的一小小部分。
这三种方法可以组合进行使用,即Combining lifecycle mechanisms
当组合使用时,那这三种方法的执行顺序是什么呢?
我们不再测试,直接看spring官方文档:
Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are called as follows:
初始化顺序
- Methods annotated with
@PostConstruct
afterPropertiesSet()
as defined by theInitializingBean
callback interface- A custom configured
init()
method
Destroy methods are called in the same order:
销毁顺序:
- Methods annotated with
@PreDestroy
destroy()
as defined by theDisposableBean
callback interface- A custom configured
destroy()
method
转载于:https://www.cnblogs.com/heliusKing/p/11385352.html
七、spring生命周期之初始化和销毁方法相关推荐
- spring生命周期管理-初始化与销毁
spring生命周期管理只涉及单例对象的管理 一初始化与销毁 1.通过设置bean的init-mothed属性指定初始化的方法,他的限制是方法无法接受任何参数,方法可以为static 2.实现Init ...
- Spring生命周期Bean初始化过程详解
Spring生命周期Bean初始化过程详解 Spring 容器初始化 Spring Bean初始化 BeanFactory和FactoryBean 源码分析 Bean的实例化 preInstantia ...
- spring-bean生命周期之初始化和销毁的三种方式
1,注解bean之指定init-method/destroy-method 这种方式spring注解之@Bean注解,这边再简单演示如下: 配置类中增加一个bean如下: /*** 定义一个bean对 ...
- spring生命周期七个过程_Spring杂文(三)Spring循环引用
众所周知spring在默认单例的情况下是支持循环引用的 Appconfig.java类的代码 @Configurable @ComponentScan("com.sadow") p ...
- 【Java从0到架构师】Spring - 生命周期、代理
生命周期.代理 bean 的生命周期 代理 业务层的一些问题 静态代理 (Static Proxy) 动态代理 (Dynamic Proxy) JDK 动态代理 - Proxy.newProxyIns ...
- 老司机带你从源码开始撸Spring生命周期!!!
导读 Spring在Java Web方面有着举足轻重的地位,spring的源码设计更是被很多开发者所惊叹,巧妙的设计,精细的构思,都注定他的地位.今天陈某大言不惭的带你来从源码角度解析Spring的生 ...
- 面试题------Spring中Bean的初始化以及销毁init-method、destory-method
面试题------Spring中Bean的生命周期 通过Spring工厂,可以控制bean的生命周期. 在xml中配置Bean的初始化和销毁方法 通过init-method属性指定初始化后的调用方法. ...
- Spring生命周期详解
导读 Spring中Bean的生命周期从容器的启动到停止,涉及到的源码主要是在org.springframework.context.support.AbstractApplicationContex ...
- 【SSM - Spring篇01】spring详细概述,Spring体系结构,bean、property属性,Spring生命周期方法
文章目录 1. Spring介绍 2. Spring体系架构 2.1 Spring核心容器(Core Container) 2.2 数据访问/集成(Data Access/Integration) 2 ...
最新文章
- matplotlib 可视化必知必会富文本绘制方法
- python脚本多线程爬虫爬电脑壁纸
- HTTP长连接短连接
- Android开源项目:GifView——Android显示GIF动画
- 工作中常用的kafka命令
- java项目 异常如何解决_Java项目中常见的异常处理
- 关于COCOS2D-X 中的音乐与音效应用的备注
- scala中的match
- java压缩包上传,解压,预览(利用editor.md和Jstree实现)和下载
- 20200105每日一句
- 8086汇编_常用指令
- layui table切换html,解决Layui中切换tab时table样式错乱问题
- nmn抗衰老是骗局吗,美国nmn骗局,正面解答
- ubuntu 16.04登陆界面循环(输密码闪屏回到登陆界面)排查过程
- 使用MIT JWI(Java WordNet Interface)查询WordNet反义词
- 【医学信息学】研究和统计——队列研究和数据分析
- ROS工业机器人和工业自动化竞赛ARIAC 2021即将启动
- jQuery之属性操作
- 洛谷2336 BZOJ2754 SCOI2012 喵星球上的点名 SA 莫队 二分
- 惠普HP LaserJet Enterprise 500 M551xh 打印机驱动
热门文章
- 推荐!京东开源姿态跟踪新框架LightTrack!
- 零基础应该先学习 java、php、前端 还是 python?
- 计算机视觉论文-2021-07-02
- 李飞飞组开源大型室内场景的模拟环境iGibson
- 资源下载| 机器学习经典书籍《统计学习方法》(Python3.6)代码实现(及课件)
- php1045无法登录mysql_phpstudy安装完成后打开phpmyadmin提示#1045 无法登录 MySQL 服务器...
- android tv nugat,GitHub - GongXunYoung/Android-tv-widget: Android tv,盒子,投影仪 控件
- 配置连接池连接oracle,Oracle连接池怎么配置
- 第五章应用系统安全基础备考要点及真题分布
- 1900-01-01t00:00:00+08:00 java_日期格式转换 java 2016-09-03T00:00:00.000+08:00