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 - IoC(10): 生命周期的更多相关文章

  1. Spring Bean的生命周期、Spring MVC的工作流程、IOC,AOP

    1.Spring Bean的生命周期? (1)构造方法实例化bean. (2)构造方法设置对象属性. (3)是否实现aware接口,三种接口(BeanNameAware,BeanFactoryAwar ...

  2. 第37讲 谈谈Spring Bean的生命周期和作用域

    在企业应用软件开发中,Java 是毫无争议的主流语言,开放的 Java EE 规范和强大的开源框架功不可没,其中 Spring 毫无疑问已经成为企业软件开发的事实标准之一.今天这一讲,我将补充 Spr ...

  3. Spring Bean的生命周期,《Spring 实战》书中的官方说法

    连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...

  4. 在web.xml中配置监听器来控制ioc容器生命周期

    5.整合关键-在web.xml中配置监听器来控制ioc容器生命周期 原因: 1.配置的组件太多,需保障单实例 2.项目停止后,ioc容器也需要关掉,降低对内存资源的占用. 项目启动创建容器,项目停止销 ...

  5. Spring学习手札(四)谈谈Spring Bean的生命周期及作用域

    在Spring中,那些组成应用程序的主体以及由Spring IoC容器所管理的对象,被称之为Bean.Bean与应用程序中其他对象(比如自己创建类)的区别就是,Bean是由IoC容器创建于销毁的.在S ...

  6. Spring Bean的生命周期详解(转)

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  7. Spring5源码解析-论Spring DispatcherServlet的生命周期

    Spring Web框架架构的主要部分是DispatcherServlet.也就是本文中重点介绍的对象. 在本文的第一部分中,我们将看到基于Spring的DispatcherServlet的主要概念: ...

  8. 谈谈Spring bean的生命周期(一)

    简介 本片文章主要讲Spring IOC容器中 bean 的生命周期 Spring bean 生命周期 Spring 中bean的声明周期 可以分为如下4个阶段: 实例化阶段--Instantiati ...

  9. Spring应用上下文生命周期

    Spring应用上下文生命周期整体分成四个阶段 ConfigurableApplicationContext#refresh,加载或者刷新持久化配置 ConfigurableApplicationCo ...

随机推荐

  1. EAS_Table

    SHR人力 员工表 T_BD_PERSON fbirthday 出生日期 femployeetypeid       员工状态   员工状态  T_HR_BDEMPLOYEETYPE        T ...

  2. python脚本退出后 不应该为负值

    Python sys.exit的退出代码 sys.exit(n)介绍 功能:执行到主程序末尾,解释器自动退出,但是如果需要中途退出程序,可以调用sys.exit函数,带有一个可选的整数参数返回给调用它 ...

  3. 1139: [POI2009]Wie

    1139: [POI2009]Wie https://www.lydsy.com/JudgeOnline/problem.php?id=1139 分析: Dijkstra.状压最短路,dis[i][j ...

  4. Cyclone IV器件的逻辑单元和逻辑阵列快

    1. 逻辑单元 (LE) 在 Cyclone IV 器件结构中是最小的逻辑单位.LE 紧密且有效的提供了高级功能的逻辑使用.每个 LE 有以下特性:一个四口输入的查找表 (LUT),以实现四种变量的任 ...

  5. oracle 开启归档日志模式

    摘自:https://www.jianshu.com/p/f8c0e9309ce2 在默认情况下,oracle数据库是在非归日志档模式中创建的,在非归档日志模式中,进行日志切换时会直接重写redo l ...

  6. 「日常训练」Greedy Arkady (CFR476D2C)

    不用问为啥完全一致,那个CSDN的也是我的,我搬过来了而已. 题意(Codeforces 965C) $k$人分$n$个糖果,每个糖果至多属于1个人.A某人是第一个拿糖果的.(这点很重要!!) 他$x ...

  7. Asp.net core中Migration工具使用的交流分享

    a,ul>li,em{ color:deeppink !important; } h2>a{ text-decoration:none; } ul>li{ padding:3px; ...

  8. linux学习总结-----web前端①

    <html> <head> <title></title> <meta charset='utf-8'/> ... </head> ...

  9. CSP201312-1:出现次数最多的数

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的“计算机职业资格认证”考试,针对计算机软件开发. ...

  10. ZooKeeper完全分布式安装与配置

    Apache ZooKeeper是一个为分布式应用所设计开源协调服务,其设计目是为了减轻分布式应用程序所承担的协调任务.可以为用户提供同步.配置管理.分组和命名服务. 1.环境说明 在三台装有cent ...