一、理解 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 的生命周期的更多相关文章

  1. Spring学习-- IOC 容器中 bean 的生命周期

    Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...

  2. Spring(十二):IOC容器中Bean的生命周期方法

    IOC容器中Bean的生命周期方法 1)Spring IOC容器可以管理Bean的声明周期,Spring允许在Bean生命周期的特定点执行定制的任务. 2)Spring IOC容器对Bean的生命周期 ...

  3. spring IOC 容器中 Bean 的生命周期

    IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...

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

    1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...

  5. IOC容器中bean的生命周期

    一.Bean的生命周期 Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务. Spring IOC容器对Bean的生命周期进行管理的过程如下: (1).通 ...

  6. [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)

    Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...

  8. 7 -- Spring的基本用法 -- 9...容器中Bean的生命周期

    7.9 容器中Bean的生命周期 Spring可以管理singleton作用域的Bean的生命周期,Spring可以精确地知道该Bean何时被创建,何时被初始化完成.容器何时准备销毁该Bean实例. ...

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

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

随机推荐

  1. 进程、线程、轻量级进程、协程与 go 的 goroutine【转载+整理】

    本文内容 进程 线程 协程 Go 中的 goroutine 参考资料 最近,看一些文章,提到"协程"的概念,心想,进程,线程,协程,前两个很容易,任何一本关于操作系统的书都有说,开 ...

  2. 解决部份机型toast不显示问题

    问题:部份机型不显示toast 解决方案: 1.自己在设置里面去允许通知,但是显然客户会说别的app都可以,so 2.自定义解决.查看toast的源码发现其附着在window上 源码下载地址:http ...

  3. parallels无法启动之大乌龙-流水账版

    欢迎访问我的blog:blog.thinkinside.me     早上到公司,像往日一样,开电脑倒茶喝水. 回到座位打开parallels desktop,发现不对,打开PD非常的慢.显示正在初始 ...

  4. SDN论坛看到BW的问题及相关解答

    SDN论坛看到BW的问题及相关解答 链接: http://blog.sina.com.cn/s/blog_5c58e3c70100r1ou.html 现在有一个 QUERY 运行十分慢 , 所以我想在 ...

  5. Python: 拷贝函数签名

    使用场景有很多,比如C API在Python下很多都变成了(*args, **kwargs)的参数,这时候可能需要为其添加一个更严格签名来约束参数. 查了许多资料,能有效的拷贝函数签名貌似只能通过动态 ...

  6. Visual Studio 新建项目报错" this template attempted to load component assembly 'NuGet.VisualStudio.Interop, ….".

    "Error: this template attempted to load component assembly 'NuGet.VisualStudio.Interop, Version ...

  7. sql:pivot unpivot

    pivot  行转列 unpivot  列转行 源码跑步起来 这是能跑起来的 源码转自 http://www.cnblogs.com/zhangzt/archive/2010/07/29/178782 ...

  8. shell 复习

    grep -v zip$  -v 逻辑否  $以zip结尾 (^开头) -n str不空,-z str 空

  9. dlib库使用

    最近的工作中用到了dlib这个库,该库是一个机器学习的开源库,使用起来很方便,直接包含头文件即可,并且不依赖于其他库(自带图像编解码库源码).不过由于是开源的,所以bug多少有一些,我在example ...

  10. PHP 常见语法 集合

    1.die()与exit()的真正区别 die 为 exit 的别名, 执行过程 将释放内存,停止代码执行 echo "begin exec <br/>"; show( ...