Spring 容器可以管理 singleton 作用域 Bean 的生命周期,容器能够跟踪 Bean 实例的创建、销毁。管理 Bean 生命周期行为主要有两个时机:
  注入 Bean 的依赖关系之后
  即将销毁 Bean 之间

依赖关系注入之后的行为

有三种方式可以在 Bean 的所有属性设置成功后执行特定的行为:
  实现 org.springframework.beans.factory.InitializingBean 接口
  使用 init-method 属性
  使用 @PostConstruct 注解

实现 InitializingBean 接口的示例

Bean 的定义:

  1. public class ExampleBean implements InitializingBean {
  2.  
  3. private String field1;
  4. private String field2;
  5.  
  6. public void setField1(String field1) {
  7. this.field1 = field1;
  8. System.out.println("field1 was set.");
  9. }
  10.  
  11. public void setField2(String field2) {
  12. this.field1 = field2;
  13. System.out.println("field2 was set.");
  14. }
  15.  
  16. public ExampleBean() {
  17. System.out.println("In ExampleBean Constructor.");
  18. }
  19.  
  20. public void afterPropertiesSet() throws Exception {
  21. System.out.println("All properties were set.");
  22. }
  23.  
  24. }

Spring 配置:

  1. <bean id="eb" class="com.huey.dream.bean.ExampleBean">
  2. <property name="field1" value=""/>
  3. <property name="field2" value=""/>
  4. </bean>

测试方法:

  1. @Test
  2. public void testLifecycle() throws Exception {
  3. ApplicationContext appCtx =
  4. new ClassPathXmlApplicationContext("applicationContext.xml");
  5. }

结果输出:

  1. In ExampleBean Constructor.
  2. field1 was set.
  3. field2 was set.
  4. All properties were set.

使用 init-method 属性的示例

Bean 的定义:

  1. public class ExampleBean {
  2.  
  3. private String field1;
  4. private String field2;
  5.  
  6. public void setField1(String field1) {
  7. this.field1 = field1;
  8. System.out.println("field1 was set.");
  9. }
  10.  
  11. public void setField2(String field2) {
  12. this.field1 = field2;
  13. System.out.println("field2 was set.");
  14. }
  15.  
  16. public ExampleBean() {
  17. System.out.println("In ExampleBean Constructor.");
  18. }
  19.  
  20. public void init() throws Exception {
  21. System.out.println("In init method.");
  22. }
  23.  
  24. }

Spring 配置:

  1. <bean id="eb" class="com.huey.dream.bean.ExampleBean" init-method="init">
  2. <property name="field1" value=""/>
  3. <property name="field2" value=""/>
  4. </bean>

使用 @PostConstruct 注解

Bean 的定义:

  1. public class ExampleBean {
  2.  
  3. private String field1;
  4. private String field2;
  5.  
  6. public void setField1(String field1) {
  7. this.field1 = field1;
  8. System.out.println("field1 was set.");
  9. }
  10.  
  11. public void setField2(String field2) {
  12. this.field1 = field2;
  13. System.out.println("field2 was set.");
  14. }
  15.  
  16. public ExampleBean() {
  17. System.out.println("In ExampleBean Constructor.");
  18. }
  19.  
  20. @PostConstruct
  21. public void init() throws Exception {
  22. System.out.println("In init method.");
  23. }
  24.  
  25. }

Spring 配置:

  1. <bean id="eb" class="com.huey.dream.bean.ExampleBean">
  2. <property name="field1" value=""/>
  3. <property name="field2" value=""/>
  4. </bean>

Bean 销毁之前的行为

与定制初始化行为类似,也有三种方式可以在 Bean 实例销毁前执行特定的行为:
  实现 org.springframework.beans.factory.DisposableBean 接口
  使用 destroy-method 属性
  使用 @PreDestroy 注解

Spring(3.2.3) - Beans(10): 生命周期的更多相关文章

  1. Spring中bean的作用域与生命周期

    在 Spring 中,那些组成应用程序的主体及由 Spring IOC 容器所管理的对象,被称之为 bean.简单地讲,bean 就是由 IOC 容器初始化.装配及管理的对象,除此之外,bean 就与 ...

  2. Spring中Bean的作用域和生命周期

    作用域的种类 Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域.Spring3 为 Bean 定义了五种作用域,具体如下. 1)singleton 单例模式,使用 sing ...

  3. Spring中与bean有关的生命周期

    前言 记得以前的时候,每次提起Spring中的bean相关的生命周期时,内心都无比的恐惧,因为好像有很多,自己又理不清楚,然后看网上的帖子,好像都是那么一套,什么beanFactory啊,aware接 ...

  4. 详解Spring中Bean的作用域与生命周期

    摘要:在利用Spring进行IOC配置时,关于bean的配置和使用一直都是比较重要的一部分,同时如何合理的使用和创建bean对象,也是小伙伴们在学习和使用Spring时需要注意的部分,所以这一篇文章我 ...

  5. Spring中Bean的作用域、生命周期

                                   Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...

  6. Spring - IoC(10): 生命周期

    Spring 容器可以管理 singleton 作用域 Bean 的生命周期,容器能够跟踪 Bean 实例的创建.销毁.管理 Bean 生命周期行为主要有两个时机: 注入 Bean 的依赖关系之后 即 ...

  7. 从启动日志看Spring IOC的初始化和Bean生命周期

    一.Tomcat中启动IoC容器的日志 启动Tomcat等容器时,控制台每次都打印出一些日志. 最近刚好在研究Spring源码,所以换个角度,从启动日志来简单的看看Spring的初始化过程! 以下是T ...

  8. Spring之Bean的作用域与生命周期

    在前面博客中提到容器启动获得BeanDefinition对象中有一个scope 属性.该属性控制着bean对象的作用域.本章节介绍Bean的作用域及生命周期,了解bean是怎么来的又怎么没的. 一.B ...

  9. Spring ( 三 ) Spring的Bean的装配与生命周期、专用测试

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.对象的生命周期 1.IOC之Bean的生命周期 创建带有生命周期方法的bean public cla ...

随机推荐

  1. listview禁止双击一条之后选中复选框按钮的方法

    this.listViewUsers.SelectedItems[0].Checked = !this.listViewUsers.SelectedItems[0].Checked;

  2. UVaLive 7361 Immortal Porpoises (矩阵快速幂)

    题意:求Fibonacci的第 n 项. 析:矩阵快速幂,如果不懂请看http://www.cnblogs.com/dwtfukgv/articles/5595078.html 是不是很好懂呢. 代码 ...

  3. 【转】【React Native开发】

    [React Native开发]React Native控件之ListView组件讲解以及最齐全实例(19)  [React Native开发]React Native控件之Touchable*系列组 ...

  4. 配置错误 在唯一密钥属性“fileExtension”设置为“.log”时,无法添加类型为“mimeMap”的重复集合项

    错误提示: 配置错误 在唯一密钥属性“fileExtension”设置为“.log”时,无法添加类型为“mimeMap”的重复集合项 配置文件 \\?\D:\www\abc\web.config 出现 ...

  5. Winform-控制边框显示属性

  6. Js Pattern - Namespace Pattern

    bad code // BEFORE: 5 globals // Warning: antipattern // constructors function Parent() {} function ...

  7. Javascript Design Patterns - Js Class

    JavaScript is a class-less language, however classes can be simulated using functions. eg: // A car ...

  8. delphi 13 打印相关

    打印 页面设置 打印预览 文档属性     //---------------------------------------------------------------------------- ...

  9. Ruby on Rails Tutorial 第六章 用户模型

    1.用户模型(1)数据库迁移Rails默认使用关系数据库存储数据,数据库中的表有数据行组成,每一行都有相应的列,对应数据属性.把列名命名为相应的名字后,ActiveRecord会自动把他们识别为用户对 ...

  10. eclipse中,把java函数代码折叠/展开

    首先,在eclipse 中开启设置代码折叠功能 1. windows->perferences->General->Editors->Structured Text Edito ...