Spring学习总结(4)-Spring生命周期的回调
参考文档:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-lifecycle
Spring官方文档是这么写的:
Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are called as follows:
Methods annotated with
@PostConstructafterPropertiesSet()as defined by theInitializingBeancallback interfaceA custom configured
init()method
Destroy methods are called in the same order:
Methods annotated with
@PreDestroydestroy()as defined by theDisposableBeancallback interfaceA custom configured
destroy()method
1、Spring生命周期初始化阶段的回调:
1.1 在方法上使用 @PostConstruct 注解,这个方法会在类的构造方法之后执行。
1.2 继承InitializingBean接口,并实现afterPropertiesSet()方法,是接口的回调方法,页会在构造方法之后执行,但执行顺序会在@PostConstruct注解方法之后。
1.3 在类中写一个 init() 方法,但需要使用XML配置上写init-method="init",如:<bean id="**" class="***" init-method="init">
代码如下:
@Service
public class UserServiceImpl implements UserService, InitializingBean { //构造方法
private UserServiceImpl(){
System.out.println("构造方法");
} // Service的方法
public String getUser(){
System.out.println("我是getUser");
return "11";
} @PostConstruct
public void postConstruct(){
System.out.println("PostConstruct");
} public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");
} }
测试:在测试用例中调用UserServiceImpl的getUser方法,输出的结果如下:
构造方法
PostConstruct
afterPropertiesSet
我是getUser
总结:这三种方法的作用是,构造方法里不好实现的内容,都可以使用这三种方法实现,起到类初始化的加载必要内容的作用。
推荐使用1.1的方法,方便好用。1.2的方法需要额外继承接口,对类的结构会有破坏性,不推荐使用。因现在追求零配置文件,所以不推荐使用1.3的方法,所以我也没写。
2、Spring生命周期销毁阶段的回调:
2.1 在方法上使用注解@PreDestroy
2.2 继承DisposableBean接口,并实现destroy()方法
2.3 在类中写一个 destroy() 方法,并在XML文件中的bean里配置destroy-method
销毁阶段我没写例子,大家可以自行尝试。
<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />
4、lazy懒加载
@Lazy 表示在容器初始化时不加载,在真正使用时才把类初始化。
Spring学习总结(4)-Spring生命周期的回调的更多相关文章
- Spring 学习笔记---Bean的生命周期
生命周期图解 由于Bean的生命周期经历的阶段比较多,我们将通过一个图形化的方式进行描述.下图描述了BeanFactory中Bean生命周期的完整过程: Bean 的生命周期从Spring容器着手实例 ...
- Spring Environment(三)生命周期
Spring Environment(三)生命周期 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...
- (转)Spring管理的Bean的生命周期
http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...
- Spring 容器中 Bean 的生命周期
Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...
- Spring学习1:Spring基本特性
http://longliqiang88.github.io/2015/08/14/Spring%E5%AD%A6%E4%B9%A01%EF%BC%9ASpring%E5%9F%BA%E6%9C%AC ...
- Spring 学习笔记(2) Spring Bean
一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...
- MAVEN学习笔记之Maven生命周期和插件简介(3)
MAVEN学习笔记之Maven生命周期和插件简介(3) clean compile site三套生命周期相互独立. clean pre-clean 执行清理前的工作 clean 清理上一次构建生成的所 ...
- android官方Api 理解Activity生命周期的回调机制(适合有基础的人看)
原文地址:http://www.android-doc.com/training/basics/activity-lifecycle/starting.html#lifecycle-states 此处 ...
- Activity生命周期的回调,你应该知道得很多其它!--Android源代码剖析(下)
转载请标明原文地址:http://blog.csdn.net/yalinfendou/article/details/46910811[yalinfendou的博客] ...
- spring深入学习(二)-----bean的生命周期、IOC容器bean装配
bean的生命周期 1.实例化Bean对于BeanFactory容器,当客户向容器请求一个尚未初始化的bean时,或初始化bean的时候需要注入另一个尚未初始化的依赖时,容器就会调用createBea ...
随机推荐
- 隐写术工具之binwalk
0x00Binwalk介绍 Binwalk是用于搜索给定二进制镜像文件以获取嵌入的文件和代码的工具. 具体来说,它被设计用于识别嵌入固件镜像内的文件和代码. Binwalk使用libmagic库,因此 ...
- day64 django模型层
目录 一.单表操作(增删改) 二.必知必会13个方法 三.查看内部的sql语句的方法 四.神奇的双下划线查询 五.一对多外键的增删改查 六.多对多外键的增删改查 七.正反向查询概念 八.多表查询 1 ...
- 05 Vue项目搭建
Vue-CLI 项目搭建 1.环境搭建 安装node 官网下载安装包,傻瓜式安装:https://nodejs.org/zh-cn/ 安装cnpm npm install -g cnpm --regi ...
- 龙芯开源社区上线.NET主页
龙芯团队从2019年7 月份开始着手.NET Core的MIPS64支持研发,经过将近一年的研发,在2020年6月18日完成了里程碑性的工作,在github CoreCLR 仓库:https://gi ...
- shell专题(十):Shell工具(重点)
10.1 cut cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的.cut 命令从文件的每一行剪切字节.字符和字段并将这些字节.字符和字段输出. 1.基本用法 cut [选项参数] fi ...
- python面试题:redis数据库
来源链接: https://www.cnblogs.com/jasontec/p/9699242.html https://www.cnblogs.com/Java3y/p/10266306.html ...
- python 装饰器(八):装饰器基础(四)types.MethodType的作用
1 types.MethodType的作用—添加实例方法 import types class cla(object): def __init__(self, name, age): self.nam ...
- How to use the function of bind
The usage of bind is to define a specified scope for called function. Because the key this is easy ...
- 定时器三----js定时器
方法一: var t; //初始化定时器 $(function(){ init_fun_timer1(); }); ...
- MYSQL JSON字段操作
create CREATE TABLE t_test ( salary_data json NULL COMMENT 'JSON类型字段' ); -- insert INSERT INTO t_tes ...