参考文档: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. 一个简单的webAPI调用

    1.新建一个ASP.NET Web应用程序. 2.选择空模板,WebAPI. 3.在Models文件夹添加Product类. 4.添加空控制器ProductController. 5.ProductC ...

  2. SQLserver , MySQL的区别和各自的一些简单方法案列

    SQL Server数据库和MySQL数据库有什么区别呢?今天我们来分析一下这两种数据库的不同之处以及这两种数据库的一些简单用途:SQL Server数据库和MySQL数据库有什么区别: 对于程序开发 ...

  3. (数据科学学习手札89)geopandas&geoplot近期重要更新

    本文示例代码及数据已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 最近一段时间(本文写作于2020-07-1 ...

  4. MySQL分库分表的原则

    一.分表 当一个表的数据达到几千万条的时候,每一次查询都会花费更长的时间,如果这时候在使用链表查询,那么我想应该会实在那里,那么我们应该如何解决这个问题呢? 1.为什么要分表: 分表的目的就是为了解决 ...

  5. python---Flask使用教程-加载静态文件

    flask的静态文件,一般放在static目录下,前端页面放在templates下(而且这两个名字是定死的(static,templates)),目录结构如图: 模板(index.html)里加载静态 ...

  6. java中AQS源码分析

    AQS内部采用CLH队列.CLH队列是由节点组成.内部的Node节点包含的状态有 static final int CANCELLED =  1; static final int SIGNAL    ...

  7. noi linux gedit 配置(c++环境)

    基本配置 方法一 查看所有命令: gsettings list-recursively | grep -i gedit 命令解释 gsettings set org.gnome.gedit.prefe ...

  8. 并发编程AQS----共享锁

    Semaphore Semaphore 字面意思是信号量的意思,它的作用是控制访问特定资源的线程数目.应用场景:资源访问,服务限流. Semaphore 实现AbstractQueuedSynchro ...

  9. C++语法小记---string和int的相互转换

    string和int的相互转换 string转int istringstream is(""); //构造输入字符串流,流的内容初始化为“12”的字符串 int i; is > ...

  10. [spring] -- bean作用域跟生命周期篇

    作用域 singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的. prototype : 每次请求都会创建一个新的 bean 实例. request : 每一次HT ...