Spring Bean InitializingBean和DisposableBean实例
- 对于Bean实现 InitializingBean,它将运行 afterPropertiesSet()在所有的 bean 属性被设置之后。
- 对于 Bean 实现了DisposableBean,它将运行 destroy()在 Spring 容器释放该 bean 之后。
示例
package com.yiibai.customer.services; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; public class CustomerService implements InitializingBean, DisposableBean
{
String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public void afterPropertiesSet() throws Exception {
System.out.println("Init method after properties are set : " + message);
} public void destroy() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up");
} }
File : applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.yiibai.customer.services.CustomerService">
<property name="message" value="I'm property message" />
</bean> </beans>
执行它
package com.yiibai.common; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yiibai.customer.services.CustomerService; public class App
{
public static void main( String[] args )
{
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}); CustomerService cust = (CustomerService)context.getBean("customerService"); System.out.println(cust); context.close();
}
}
输出结果
Init method after properties are set : I'm property message
com.yiibai.customer.services.CustomerService@4090c06f
Spring Container is destroy! Customer clean up
不建议使用InitializingBean和DisposableBean的接口,因为它将你的代码紧耦合到 Spring 代码中。 一个更好的做法应该是在bean的配置文件属性指定init-method和destroy-method。
Spring Bean InitializingBean和DisposableBean实例的更多相关文章
- Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例
实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...
- Spring Bean init-method 和 destroy-method实例
在Spring中,可以使用 init-method 和 destroy-method 在bean 配置文件属性用于在bean初始化和销毁某些动作时.这是用来替代 InitializingBean和Di ...
- Spring的InitializingBean与DisposableBean方法
在bean初始化的时候,将所有显示提供的属性设置完毕后调用这个方法 org.springframework.beans.factory.InitializingBean#afterProperties ...
- Spring Bean生命周期,好像人的一生。。
大家好,我是老三,上节我们手撸了一个简单的IOC容器五分钟,手撸一个Spring容器!,这节我们来看一看Spring中Bean的生命周期,我发现,和人的一生真的很像. 简单说说IoC和Bean IoC ...
- Spring Bean 生命周期之destroy——终极信仰
上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P ...
- 【Spring注解驱动开发】使用InitializingBean和DisposableBean来管理bean的生命周期,你真的了解吗?
写在前面 在<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!>一文中,我们讲述了如何使用@Bean注解来指定bean初始化和销毁的方法.具体的 ...
- Spring中Bean的生命中期与InitializingBean和DisposableBean接口
Spring提供了一些标志接口,用来改变BeanFactory中的bean的行为.它们包括InitializingBean和DisposableBean.实现这些接口将会导致BeanFactory调用 ...
- Spring Bean Life Cycle Methods – InitializingBean, DisposableBean, @PostConstruct, @PreDestroy and *Aware interfaces
Spring Beans are the most important part of any Spring application. Spring ApplicationContext is res ...
- Spring bean 实现InitializingBean和DisposableBean接口实现初始化和销毁前操作
# InitializingBean接口> Spring Bean 实现这个接口,重写afterPropertiesSet方法,这样spring初始化完这个实体类后会调用这个方法```@Over ...
随机推荐
- ovirt系统磁盘删除后清理功能验证步骤
测试步骤主要是针对ovirt系统磁盘的‘删除后清理’功能,如下图所示: 测试如下两种方式: 预置条件: 搭建iscsi服务器,且划分一个11G的盘 勾选删除后清理操作步骤:1 .在linux虚拟机 d ...
- linux下使用privoxy将socks转为http代理
此博客不在更新,我的博客新地址:www.liuquanhao.com ----------------------------------------------------------------- ...
- 学习Leader选举算法
读书笔记:<从Paxos到Zookeeper 分布式一致性原理与实践> 选举的前提约定 观察者不参与选举,只有跟随者才参与选举. 优先选事务ID(ZXID)大的,事务Id相同再优先选服务器 ...
- 20165301 2017-2018-2 《Java程序设计》第九周学习总结
20165301 2017-2018-2 <Java程序设计>第九周学习总结 教材学习内容总结 第十三章:Java网络编程 URL类 通常包含三部分信息:协议.地址.资源 协议必须是URL ...
- Android学习笔记(四) 定时器Timer
Android考虑到线程安全问题,不允许在线程中执行UI线程. 所以在线程中不允许有UI操作 可以利用Handler机制来接收Timer每隔一秒发出的信息,也可以直接利用handler机制的 1.方法 ...
- 【hdoj_1865】1sting(递推+大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1865 本题的关键是找递推关系式,由题目,可知前几个序列的结果,序列长度为n=1,2,3,4,5的结果分别是 ...
- Mybatis的关联映射案例
主要是对之前学习的关联映射做一个案例,自己动手实践一下,可以理解的更好一点. 开发环境 开发工具:idea Java环境: jdk1.8.0_121 数据库:SQLServer 项目结构,里面包含了三 ...
- Linux文件系统的详解
这里以 EXT2 文件系统为例 在Linux下,一个磁盘的最前面是MBR,大小为512Byte 在每一个分区下,第一部分是boot sector,接下来是super block,再接下来是inode, ...
- JS~jwPlayer为js预留的回调方法大总结
对于一个成功的产品,它是对外封闭的,但又是对外开放的,这句话并不矛盾,让我来说一下,说它对外封闭是因为它本身的代码不允许你去修改,而说它开放,是因为它为我们定义了很多API,或者叫回调方法,对于jwp ...
- 洛谷——P1894 [USACO4.2]完美的牛栏The Perfect Stall
P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...