Spring重点—— IOC 容器中 Bean 的生命周期
一、理解 Bean 的生命周期,对学习 Spring 的整个运行流程有极大的帮助。
二、在 IOC 容器中,Bean 的生命周期由 Spring IOC 容器进行管理。
三、在没有添加后置处理器的情况下 Bean 的生命周期
1.通过构造器或工厂方法创建 Bean 的实例
2.为 Bean 的属性设置值好对其他 Bean 的引用
3.调用 Bean 的初始化方法
4.Bean 可以使用了
5.当容器关闭时,调用 Bean 的销毁方法
*在 Bean 的声明里设置 init-method 和 destroy-method 属性,为 Bean 指定初始化和销毁方法。
例如:
/**
* @author solverpeng
* @create 2016-07-18-20:42
*/
public class Life {
private String lifeName; public void setLifeName(String lifeName) {
System.out.println("setLifeName....");
this.lifeName = lifeName;
} public Life() {
System.out.println("constructor....");
} public void initMethod() {
System.out.println("initMethod....");
} public void destroyMethod() {
System.out.println("destroyMethod....");
} public void targetMethod() {
System.out.println("targetMethod....");
} }
spring-config.xml
<bean class="com.nucsoft.spring.bean.Life" id="life" init-method="initMethod" destroy-method="destroyMethod">
<property name="lifeName" value="myLife"/>
</bean>
Test
@Test
public void test03() {
Life life = ctx.getBean(Life.class);
life.targetMethod();
}
控制台输出:
constructor....
setLifeName....
initMethod....
targetMethod....
四、Bean 后置处理器
1.Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理。
2.Bean 后置处理器对 IOC 容器里的所有 Bean 实例逐一处理。
3.具体使用:需要实现 BeanPostProcessor 接口,实现 postProcessBeforeInitialization(Object bean, String beanName) 和 postProcessAfterInitialization(Object bean, String beanName) 两个方法。
分别在 初始化方法前后被调用。
五、添加 Bean 后置处理器后的 Bean 的生命周期
1.通过构造器或工厂方法创建 Bean 的实例
2.为 Bean 的属性设置值和对其他 Bean 的引用
3.将 Bean 实例传递给 bean 后置处理器的 postProcessBeforeInitialization() 方法
4.调用 Bean 的初始化方法
5.将 Bean 实例传递给 bean 后置处理器的 postProcessAfterInitialization() 方法。
6.使用 Bean
7.当容器关闭时,调用 Bean 的销毁方法。
例如:
/**
* @author solverpeng
* @create 2016-07-18-20:42
*/
public class Life {
private String lifeName; public void setLifeName(String lifeName) {
System.out.println("setLifeName....");
this.lifeName = lifeName;
} public Life() {
System.out.println("constructor....");
} public void initMethod() {
System.out.println("initMethod....");
} public void destroyMethod() {
System.out.println("destroyMethod....");
} public void targetMethod() {
System.out.println("targetMethod....");
} }
Life.java
/**
* @author solverpeng
* @create 2016-07-18-20:58
*/
public class MyBeanPostProcessor implements BeanPostProcessor{ @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof Life) {
System.out.println("life's postProcessBeforeInitialization....");
}
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof Life) {
System.out.println("life's postProcessAfterInitialization....");
}
return bean;
}
}
MyBeanPostProcessor.java
<bean class="com.nucsoft.spring.processor.MyBeanPostProcessor"/> <bean class="com.nucsoft.spring.bean.Life" id="life" init-method="initMethod" destroy-method="destroyMethod">
<property name="lifeName" value="myLife"/>
</bean>
Test
@Test
public void test03() {
Life life = ctx.getBean(Life.class);
life.targetMethod();
}
控制台输出:
constructor....
setLifeName....
life's postProcessBeforeInitialization....
initMethod....
life's postProcessAfterInitialization....
targetMethod....
Spring重点—— IOC 容器中 Bean 的生命周期的更多相关文章
- Spring学习-- IOC 容器中 bean 的生命周期
Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...
- Spring(十二):IOC容器中Bean的生命周期方法
IOC容器中Bean的生命周期方法 1)Spring IOC容器可以管理Bean的声明周期,Spring允许在Bean生命周期的特定点执行定制的任务. 2)Spring IOC容器对Bean的生命周期 ...
- spring IOC 容器中 Bean 的生命周期
IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...
- Spring IOC容器中Bean的生命周期
1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...
- IOC容器中bean的生命周期
一.Bean的生命周期 Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务. Spring IOC容器对Bean的生命周期进行管理的过程如下: (1).通 ...
- [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)
Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...
- 7 -- Spring的基本用法 -- 9...容器中Bean的生命周期
7.9 容器中Bean的生命周期 Spring可以管理singleton作用域的Bean的生命周期,Spring可以精确地知道该Bean何时被创建,何时被初始化完成.容器何时准备销毁该Bean实例. ...
- Spring 容器中 Bean 的生命周期
Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...
随机推荐
- java产生随机数的几种方式
java产生随机数的几种方式 一.在j2se里我们可以使用Math.random()方法来产生一个随机数,这个产生的随机数是0-1之间的一个double,我们可以把他乘以一定的数,比如说乘以100,他 ...
- webstorm 配合IIS使用
添加名称之后 点击apply 再点击ok 然后在打开设置 就可以配置下图的信息 我们需要在webstorm里面打开IIS部署的地址怎么设置呢? 技术交流QQ群:15129679
- Scala 深入浅出实战经典 第76讲:模式匹配下的赋值语句
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...
- MVC的JsonResult用法
在Asp.net Mvc 2中由于对数据的保护,默认情况下request为post,所以在前端请求的时候则需要以post方式request action方法: public JsonResult Ge ...
- 读写文本(.txt)文件 .NET
http://www.cnblogs.com/jx270/archive/2013/04/14/3020456.html (一) 读取文件 如果你要读取的文件内容不是很多,可以使用 File.Read ...
- IE11 Enterprise Mode
对IE11引入的Enterprise Mode进行了一些总结,对查阅的一些参考资料直接引用了英文,需要注意的地方用中文进行了一些注解.供大家参考. 1. The purpose of introduc ...
- CSS基础(一):开篇
背景 HTML是一种超文本标记语言,用来定义文档的结构和内容,例如标题.段落和列表等等,而文档内容如何渲染.如何展示,这就需要样式来修饰了.CSS正是可以与HTML很好地结合.如果将HTML比作水,那 ...
- FastSocket.Net
Overview FastSocket是一个轻量级易扩展的c#异步socket通信库,项目开始于2011年,经过近3年不断调整与改进,目前在功能和性能上均有不错的表现. 项目地址:https://gi ...
- 关于float /double、string类型的hash函数/hash表实现(转)
#include <ext/hash_map> #include <math.h> #include <stdio.h> using namespace std; ...
- C#连接mysql数据库插入数据后获取自增长主键ID值
From: http://blog.csdn.net/zbc496218/article/details/51082983 MySqlConnection conn = new MySqlConnec ...