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 ...
随机推荐
- oratop 各个指标项说明
Section 1- oratop and database/instance specifics spid :oratop's server SPID connected to inst ...
- PCM音频设备的操作(转)
对音频设备的操作主要是初始化音频设备以及往音频设备发送 PCM(Pulse Code Modulation)数据.为了方便,本文使用 ALSA(Advanced Linux Sound Archite ...
- C++ 代码换行
1.字符串太长,换行显示,怎么办?2.使用反斜杠,如下: string str = "abcd\ 1234"; 注意:反斜杠后面不准有任何字符.下一行开头的制表符不包含在整个字符串 ...
- Scala 深入浅出实战经典 第63讲:Scala中隐式类代码实战详解
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- 聊聊CSS postproccessors
阿里妈妈 @一丝 准备发布其CSSGrace,即CSS后处理插件,于是顺便聊聊CSS postprocessors. 从Rework说起 Rework是TJ大神开发的CSS预处理框架.但为什么会出 ...
- Android开发(二十八)——基础功能函数
/** * 判断事件是否在控件中 * * @param view * @param ev * @return * @see http://m.blog.csdn.net/blog/aygxylxk/8 ...
- Navi.Soft30.产品.DataWindowNet.操作手册
1概述 1.1功能简介 Sybase公司的PowerBuilder开发工具,在以前VS工具没有成事以前,是相当风光的.微软都要与其合作,学习它Db方面的技术,才成就了SQLServer数据库.PB开发 ...
- 【网络编程】——windows socket 编程
测试demo #include <winsock2.h> #include <stdio.h> #include <string.h> #include <s ...
- Spark源码系列(一)spark-submit提交作业过程
前言 折腾了很久,终于开始学习Spark的源码了,第一篇我打算讲一下Spark作业的提交过程. 这个是Spark的App运行图,它通过一个Driver来和集群通信,集群负责作业的分配.今天我要讲的是如 ...
- C primer plus 练习题 第五章
1. #include <stdio.h> #define MINU 60 int main() { int minute, hour, m; printf("请输入分钟:&qu ...