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 ...
随机推荐
- idea离线安装lombock插件
技术交流群:233513714 安装过程 1.首先找到插件包 插件包可以在两个地方下载,分别是IDEA的官方插件仓库和GitHub里lombok-intellij-plugin仓库中的release包 ...
- MFC随笔记录——1
这段时间用MFC做完了项目里的一个对图像处理(字迹匹配)的软件,通过项目的具体要求的一步一步的实现,我也学习到了很多以前困惑很久的问题,算是对自己的一个提高吧,把一些有技巧性的操作记在这里,给以后的自 ...
- 一个极为简单的requirejs实现
require和 sea的源码分析,我之前的博客有写过, 今天我想分享的是一个很简单的核心代码(不带注释和空行大概60行), 没有容错判断. require.js require函数实现用一句话概括: ...
- Selenium页面工厂+数据驱动测试框架
工程的目录结构: pom.xml文件: <?xml version="1.0" encoding="UTF-8"?><project xmln ...
- 数据库sql命令
本文为转载,原文地址:http://www.cnblogs.com/cangqiongbingchen/p/4530333.html 1.说明:创建数据库CREATE DATABASE databas ...
- Python目录链接
第1章 就这么愉快的开始吧 课时1:我和python的第一次亲密接触 一.Python3的下载与安装 二.从IDIE启动Python 三.尝试点新的东西 四.为什么会这样? 五.课时01课后习题及答案 ...
- Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】
源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...
- git - work flow
git status – Make sure your current area is clean. git pull – Get the latest version from the remote ...
- 学习MVC中出现的一个BUG
BUG描述:No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.S ...
- PAT 1087 有多少不同的值
https://pintia.cn/problem-sets/994805260223102976/problems/1038429191091781632 当自然数 n 依次取 1.2.3.…….N ...