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

依赖关系注入之后的行为

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

实现 InitializingBean 接口的示例

Bean 的定义:

public class ExampleBean implements InitializingBean {

    private String field1;
private String field2; public void setField1(String field1) {
this.field1 = field1;
System.out.println("field1 was set.");
} public void setField2(String field2) {
this.field1 = field2;
System.out.println("field2 was set.");
} public ExampleBean() {
System.out.println("In ExampleBean Constructor.");
} public void afterPropertiesSet() throws Exception {
System.out.println("All properties were set."
);
}
}

Spring 配置:

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

测试方法:

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

结果输出:

In ExampleBean Constructor.
field1 was set.
field2 was set.
All properties were set.

使用 init-method 属性的示例

Bean 的定义:

public class ExampleBean  {

    private String field1;
private String field2; public void setField1(String field1) {
this.field1 = field1;
System.out.println("field1 was set.");
} public void setField2(String field2) {
this.field1 = field2;
System.out.println("field2 was set.");
} public ExampleBean() {
System.out.println("In ExampleBean Constructor.");
} public void init() throws Exception {
System.out.println("In init method."
);
}
}

Spring 配置:

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

使用 @PostConstruct 注解

Bean 的定义:

public class ExampleBean  {

    private String field1;
private String field2; public void setField1(String field1) {
this.field1 = field1;
System.out.println("field1 was set.");
} public void setField2(String field2) {
this.field1 = field2;
System.out.println("field2 was set.");
} public ExampleBean() {
System.out.println("In ExampleBean Constructor.");
} @PostConstruct
public void init() throws Exception {
System.out.println("In init method.");
} }

Spring 配置:

<bean id="eb" class="com.huey.dream.bean.ExampleBean">
<property name="field1" value=""/>
<property name="field2" value=""/>
</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. Unity3D之空间转换学习笔记(三):3D数学

    3D数学基础 向量 向量可以看做具有方向和大小的一条线段. 比如:我们如果用点A减去点B,则可以得到一个向量,该向量的方向为点B面向点A的方向,而大小为两点的距离.这个方法在游戏开发中经常用到,比如我 ...

  2. SQL Select count(*)和Count(1)的区别和执行方式及SQL性能优化

    SQL性能优化:http://www.cnblogs.com/CareySon/category/360333.html Select count(*)和Count(1)的区别和执行方式 在SQL S ...

  3. SQL Server中如何获取当前年,月,日,时,分,秒

    分类: SQL Server  select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) ...

  4. hibernate二级缓存ehcache

    与Session相对的是,SessionFactory也提供了相应的缓存机制.SessionFactory缓存可以依据功能和目的的不同而划分为内置缓存和外置缓存. SessionFactory的内置缓 ...

  5. Linux下升级python版本

    转载自:http://lovebeyond.iteye.com/blog/1770476 CentOS下的Python版本一般都比较低,很多应用都需要升级python来完成.我装的centOS的默认的 ...

  6. Oracle数据库的版本变迁功能对比

    Oracle数据库自发布至今,也经历了一个从不稳定到稳定,从功能简单至强大的过程.从第二版开始,Oracle的每一次版本变迁,都具有里程碑意义. 1979年的夏季,RSI(Oracle公司的前身,Re ...

  7. 【HMTL】文字飞舞的美

    这是在一个大神那看到的就拿过来了,希望能够更多人能看到. 这个是效果: 源文件下载: 点 击 下 载

  8. Codeforces Round #115 A. Robot Bicorn Attack 暴力

    A. Robot Bicorn Attack Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/17 ...

  9. 异步消息总线hornetq学习-03客户端连接hornet进行jms消息的收发-非jndi方式连接

    在上节中介绍了通过jndi方式连接到hornetq服务器上,有时候由于某些原因,我们不希望通过jndi方式连接,hornetq也支持这种方式进行 以第2章节的例子为模板,我们编写了另一个获取Conne ...

  10. ApiDemo/FragmentRetainInstance 解析

    /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Versi ...