参考文档: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:

  1. Methods annotated with @PostConstruct

  2. afterPropertiesSet() as defined by the InitializingBean callback interface

  3. A custom configured init() method

Destroy methods are called in the same order:

  1. Methods annotated with @PreDestroy

  2. destroy() as defined by the DisposableBean callback interface

  3. A 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

销毁阶段我没写例子,大家可以自行尝试。

 
3、depends-on显示声明类的前后依赖关系
  这个使用方式表示两个类之间并没有显示的注入依赖关系,但beanOne在初始化时,需要获取manager的内容,比如公共变量等。
3.1 XML声明depends-on
<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />

4、lazy懒加载

  @Lazy 表示在容器初始化时不加载,在真正使用时才把类初始化。

Spring学习总结(4)-Spring生命周期的回调的更多相关文章

  1. Spring 学习笔记---Bean的生命周期

    生命周期图解 由于Bean的生命周期经历的阶段比较多,我们将通过一个图形化的方式进行描述.下图描述了BeanFactory中Bean生命周期的完整过程: Bean 的生命周期从Spring容器着手实例 ...

  2. Spring Environment(三)生命周期

    Spring Environment(三)生命周期 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...

  3. (转)Spring管理的Bean的生命周期

    http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...

  4. Spring 容器中 Bean 的生命周期

    Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...

  5. 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 ...

  6. Spring 学习笔记(2) Spring Bean

    一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...

  7. MAVEN学习笔记之Maven生命周期和插件简介(3)

    MAVEN学习笔记之Maven生命周期和插件简介(3) clean compile site三套生命周期相互独立. clean pre-clean 执行清理前的工作 clean 清理上一次构建生成的所 ...

  8. android官方Api 理解Activity生命周期的回调机制(适合有基础的人看)

    原文地址:http://www.android-doc.com/training/basics/activity-lifecycle/starting.html#lifecycle-states 此处 ...

  9. Activity生命周期的回调,你应该知道得很多其它!--Android源代码剖析(下)

            转载请标明原文地址:http://blog.csdn.net/yalinfendou/article/details/46910811[yalinfendou的博客]          ...

  10. spring深入学习(二)-----bean的生命周期、IOC容器bean装配

    bean的生命周期 1.实例化Bean对于BeanFactory容器,当客户向容器请求一个尚未初始化的bean时,或初始化bean的时候需要注入另一个尚未初始化的依赖时,容器就会调用createBea ...

随机推荐

  1. js复制内容到剪贴板格式化粘贴到excel中

    <input id="Button1" type="button" value="导出EXCEL" onclick="cop ...

  2. CSS(二)- 属性速览(含版本、继承性和简介)

    相关链接 CSS3速查表,这里面列出了所有新增的属性以及新增或者修改的属性值 css参考手册,很好地一个常用网站 CSS定位(不可继承) CSS布局(仅visibility可继承) CSS尺寸(不可继 ...

  3. scrapy shell 的使用

    是什么?:是一个终端下的调试工具,用来调试scrapy 安装ipython :pip install ipython 启动: scrapy shell + 需要请求的url 进来之后,response ...

  4. Python面向对象01 /面向对象初识、面向对象结构、类、self、实例化对象

    Python面向对象01 /面向对象初识.面向对象结构.类.self.实例化对象 目录 Python面向对象01 /面向对象初识.面向对象结构.类.self.实例化对象 1. 面向对象初识 2. 面向 ...

  5. BFC 生成 特性 解决的问题

    BFC( 块级格式化上下文 ) 块级格式化上下文,它是指一个独立的块级渲染区域, 只有 Block­level BOX 参与,该区域拥有一套 渲染规则来约束块级盒子的布局,且与区域外部无关. 如何生成 ...

  6. 使用位运算、值交换等方式反转java字符串-共四种方法

    在本文中,我们将向您展示几种在Java中将String类型的字符串字母倒序的几种方法. StringBuilder(str).reverse() char[]循环与值交换 byte循环与值交换 apa ...

  7. HTML学习分享二

        一 . 常用快捷键       ctrl + s : 保存     ctrl + x.ctrl + c.ctrl + v : 剪切.复制.粘贴     ctrl + a : 全选     sh ...

  8. ToText Basic 语法和 Crystal 语法

    本文转自http://www.softwelt.com/Know/KnowDetail-2236860.html ToText 和 CStr 是等价函数. 重载 ToText (x) ToText ( ...

  9. Disease Manangement 疾病管理

    题目描述 Alas! \(A\) set of \(D (1 <= D <= 15)\) diseases (numbered \(1..D\)) is rshning through t ...

  10. websphere8.5配置db2数据源

    websphere8.5配置db2数据源 1. 打开websphere控制台  2.进入websphere变量页面  3. 进入DB2UNIVERSAL_JDBC_DRIVER_NATIVEPATH变 ...