Spring - IoC(10): 生命周期
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): 生命周期的更多相关文章
- Spring Bean的生命周期、Spring MVC的工作流程、IOC,AOP
1.Spring Bean的生命周期? (1)构造方法实例化bean. (2)构造方法设置对象属性. (3)是否实现aware接口,三种接口(BeanNameAware,BeanFactoryAwar ...
- 第37讲 谈谈Spring Bean的生命周期和作用域
在企业应用软件开发中,Java 是毫无争议的主流语言,开放的 Java EE 规范和强大的开源框架功不可没,其中 Spring 毫无疑问已经成为企业软件开发的事实标准之一.今天这一讲,我将补充 Spr ...
- Spring Bean的生命周期,《Spring 实战》书中的官方说法
连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...
- 在web.xml中配置监听器来控制ioc容器生命周期
5.整合关键-在web.xml中配置监听器来控制ioc容器生命周期 原因: 1.配置的组件太多,需保障单实例 2.项目停止后,ioc容器也需要关掉,降低对内存资源的占用. 项目启动创建容器,项目停止销 ...
- Spring学习手札(四)谈谈Spring Bean的生命周期及作用域
在Spring中,那些组成应用程序的主体以及由Spring IoC容器所管理的对象,被称之为Bean.Bean与应用程序中其他对象(比如自己创建类)的区别就是,Bean是由IoC容器创建于销毁的.在S ...
- Spring Bean的生命周期详解(转)
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
- Spring5源码解析-论Spring DispatcherServlet的生命周期
Spring Web框架架构的主要部分是DispatcherServlet.也就是本文中重点介绍的对象. 在本文的第一部分中,我们将看到基于Spring的DispatcherServlet的主要概念: ...
- 谈谈Spring bean的生命周期(一)
简介 本片文章主要讲Spring IOC容器中 bean 的生命周期 Spring bean 生命周期 Spring 中bean的声明周期 可以分为如下4个阶段: 实例化阶段--Instantiati ...
- Spring应用上下文生命周期
Spring应用上下文生命周期整体分成四个阶段 ConfigurableApplicationContext#refresh,加载或者刷新持久化配置 ConfigurableApplicationCo ...
随机推荐
- JAVA中堆栈和内存分配详解(摘抄)
在Java中,有六个不同的地方可以存储数据: 1.寄存器:最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制. 2. 栈:存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存 ...
- jpa Specification复杂查询
public List<Receipts> test(List<String> costIds){ Specification<Receipts> specific ...
- 【jQuery】 实用 js
[jQuery] 实用 js 1. int 处理 parseInt(") // int 转换 isNaN(page) // 判断是否是int类型 2. string 处理 // C# str ...
- python爬取数据需要注意的问题
1 爬取https的网站或是接口的时候,如果是不受信用的SSL证书,会报错,需要添加如下代码,如下代码可以保证当前代码块内所有的请求都自动屏蔽ssl证书问题: import ssl # 这个是爬取ht ...
- 剖析DI
0x00.前言 当我们研究一些晦涩的源码,上网查阅资料的时候,映入眼帘的总有这么些名词:DIP.IOC.DI.DL.IOC容器这些专业名词.如果不懂这些名词背后的含义,我们内心有可能是这样的: 0x0 ...
- 编译 TensorFlow 的 C/C++ 接口
TensorFlow 的 Python 接口由于其方便性和实用性而大受欢迎,但实际应用中我们可能还需要其它编程语言的接口,本文将介绍如何编译 TensorFlow 的 C/C++ 接口. 安装环境: ...
- lintcode-95-验证二叉查找树
95-验证二叉查找树 给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二 ...
- HDU 1798 Tell me the area
http://acm.hdu.edu.cn/showproblem.php?pid=1798 Problem Description There are two circles in the ...
- Java使用泛型的困顿
原文有点儿胡说的意味,删了,有空再次更新这篇博文~
- 使用 TListView 控件(2)
本例效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...