Spring IOC -bean对象的生命周期详解
生命周期执行的过程如下:
1) spring对bean进行实例化,默认bean是单例
2) spring对bean进行依赖注入
3) 如果bean实现了BeanNameAware接口,spring将bean的id传给setBeanName()方法
4) 如果bean实现了BeanFactoryAware接口,spring将调用setBeanFactory方法,将BeanFactory实例传进来
5) 如果bean实现了ApplicationContextAware()接口,spring将调用setApplicationContext()方法将应用上下文的引用传入
6) 如果bean实现了BeanPostProcessor接口,spring将调用它们的postProcessBeforeInitialization接口方法
7) 如果bean实现了InitializingBean接口,spring将调用它们的afterPropertiesSet接口方法,类似的如果bean使用了init-method属性声明了初始化方法,改方法也会被调用
8) 如果bean实现了BeanPostProcessor接口,spring将调用它们的postProcessAfterInitialization接口方法
9) 此时bean已经准备就绪,可以被应用程序使用了,他们将一直驻留在应用上下文中,直到该应用上下文被销毁
10) 若bean实现了DisposableBean接口,spring将调用它的distroy()接口方法。同样的,如果bean使用了destroy-method属性声明了销毁方法,则该方法被调用
上面流程图当中涉及调用很多的方法,可能我们直接去理解和记忆比较困难,其实对于这么一大堆方法我们可以根据它们的特点对他们进行整理分类,下面提供一种可供大家参考的分类模型:
分类类型 | 所包含方法 |
---|---|
Bean自身的方法 | 配置文件中的init-method和destroy-method配置的方法、Bean对象自己调用的方法 |
Bean级生命周期接口方法 | BeanNameAware、BeanFactoryAware、InitializingBean、DiposableBean等接口中的方法 |
容器级生命周期接口方法 | InstantiationAwareBeanPostProcessor、BeanPostProcessor等后置处理器实现类中重写的方法 |
这时回过头再来看这些方法轮廓上就比较清晰了,记忆的时候我们可通过记忆分类的类型来理解性的记忆所涉及的方法。
其实很多时候我们并不会真的去实现上面说描述的那些接口,那么下面我们就除去那些接口针对bean的单例和非单例来描述下bean的生命周期:
一,单例管理的对象:
1.默认情况下,spring在读取xml文件的时候,就会创建对象。
2.在创建的对象的时候(先调用构造器),会去调用init-method=".."属性值中所指定的方法。
3.对象在被销毁的时候,会调用destroy-method="..."属性值中所指定的方法。(例如调用container.destroy()方法的时候)
4.lazy-init="true",可以让这个对象在第一次被访问的时候创建。
代码编写如下:
1.先写Bean类-LifeBean类:
public class LifeBean{
private String name;
private LifeBean lb; public LifeBean getLb() {
return lb;
}
public void setLb(LifeBean lb) {
this.lb = lb;
}
public LifeBean(){
System.out.println("LifeBean() 构造器");
}
public String getName() {
return name;
} public void setName(String name) {
System.out.println("setName() 方法");
this.name = name;
} public void init(){
System.out.println("this is init in lifeBean");
} public void destory(){
System.out.println("this is destory in lifeBean");
} }
2.写配置文件life.xnl:(注:配置文件时随便命名的,只要是xml文件就行~)
<?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/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean name="life" class="com.x.spring.bean.LifeBean" init-method="init" destroy-method="destory"></bean>
</beans>
3.编写测试类LifeTest:
public class LifeTest { public static void main(String[] args) {
String path = "life.xml";
ClassPathXmlApplicationContext container =
new ClassPathXmlApplicationContext(path); LifeBean l1 = (LifeBean) container.getBean("life"); //container.destroy();
}
}
看看注释掉了这一行的效果图:
在看看没注释掉的效果图:
这个就算写完了~
二,非单例管理的对象:
1.spring读取xml文件的时候,不会创建对象.
2.在每一次访问这个对象的时候,spring容器都会创建这个对象,并且调用init-method=".."属性值中所指定的方法.
3.对象销毁的时候,spring容器不会帮我们调用任何方法,因为是非单例,这个类型的对象有很多个,spring容器一旦把这个对象交给你之后,就不再管理这个对象了.
代码编写:
只需要改一点配置文件的代码:
<?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/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean name="life" class="com.x.spring.bean.LifeBean" scope="prototype" init-method="init" destroy-method="destory"></bean>
</beans>
效果图如下:
好了,这个生命周期到这里就写完了~
本文转自:https://blog.csdn.net/qq_33642117/article/details/51924653
Spring IOC -bean对象的生命周期详解的更多相关文章
- spring中Bean对象的生命周期
经过测试对于具体的一个Bean,执行的流程应该是: 1.实例化: 常见的有构造(有参.无参)实例化.静态工厂(方法是静态,通过类名.方法返回获取).实例工厂(专门有个类负责生产对象,需要在bean中配 ...
- Spring之Bean的生命周期详解
通过前面多个接口的介绍了解了Bean对象生命周期相关的方法,本文就将这些接口的方法串起来,来了解Bean的完整的生命周期.而介绍Bean的生命周期也是面试过程中经常会碰到的一个问题,如果不注意就跳 ...
- ASP.NET生命周期详解
最近一直在学习ASP.NET MVC的生命周期,发现ASP.NET MVC是建立在ASP.NET Framework基础之上的,所以原来对于ASP.NET WebForm中的很多处理流程,如管道事件等 ...
- ASP.NET生命周期详解 [转]
最近一直在学习ASP.NET MVC的生命周期,发现ASP.NET MVC是建立在ASP.NET Framework基础之上的,所以原来对于ASP.NET WebForm中的很多处理流程,如管道事件等 ...
- ASP.NET生命周期详解(转)
看到好文章需要分享. 最近一直在学习ASP.NET MVC的生命周期,发现ASP.NET MVC是建立在ASP.NET Framework基础之上的,所以原来对于ASP.NET WebForm中的很多 ...
- React—组件生命周期详解
React—组件生命周期详解 转自 明明的博客 http://blog.csdn.net/slandove/article/details/50748473 (非原创) 版权声明:转载请注明出处,欢 ...
- ASP.NT运行原理和页面生命周期详解及其应用
ASP.NT运行原理和页面生命周期详解及其应用 1. 下面是我画的一张关于asp.net运行原理和页面生命周期的一张详解图.如果你对具体不太了解,请参照博客园其他帖子.在这里我主要讲解它的实际应用. ...
- Spring Bean的生命周期详解(转)
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
- Spring 了解Bean的一生(生命周期)
转载 https://blog.csdn.net/w_linux/article/details/80086950 该篇博客就来了解IoC容器下Bean的一生吧,也可以理解为bean的生命周期. ## ...
随机推荐
- Selenium 2自动化测试实战3(函数、类和方法)
一.函数.类和方法1.函数在python中通过def关键字来定义函数 创建一个add()函数,此函数接收两个参数a,b,通过print()打印a+b的结果.调用add()函数,并且上传两个参数3,5给 ...
- HTTP Status 500 ? Internal Server Error
getWriter()和getOutputStream()不能同时调用 HTTP Status 500 ? Internal Server Error Type Exception Report Me ...
- harbor报错解决
1. [root@host-10-1-1-71 harbor]# docker login 10.1.1.71:5000Username (admin): Password: Error respon ...
- Spring Boot 自定义注册 Servlet、Filter、Listener
前言 在 Spring Boot 中已经移除了 web.xml 文件,如果需要注册添加 Servlet.Filter.Listener 为 Spring Bean,在 Spring Boot 中有两种 ...
- 微信小程序前端支付
原文地址 //index.js Page({ data: { }, //点击支付按钮进行支付 payclick: function () { var t = this; wx.login({ //获取 ...
- Json序列化日期/Date(xxxx)/ JS转化为常用日期格式
记录开发过程中的代码片段,方便日后归纳.总结,效果如图所示: 转换前: 转换后: 代码如下,需要的朋友们自取: //JS转化为json常用日期格式 function FormatToDate(v ...
- python学习之模块-模块(三)
5.6 time 模块 已经知道的常用的time方法:time.time()获取当前时间的时间戳:time.sleep(num)线程推迟指定的时间(秒)后再继续往下运行. 时间的表示方式 大致可以分为 ...
- HDU 4585 Shaolin (STL map)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- Solr 4.4.0利用dataimporthandler导入本地pdf、word等文档
1. 创建本地目录 $ mkdir /usr/local/contentplatform/solr/solr/core1/file1 $ ls -lh total 88M -rw-r--r-- tnu ...
- [转帖]Windows与Linux的命令行命令对比
Windows与Linux的命令行命令对比 https://www.cnblogs.com/sztom/p/10785140.html * Windows不区分大小写,Linux区分大小写的. sn ...