Spring回调方法DisposableBean接口
除了自定义的destroy-method.还可以实现DisposableBean接口,来回调bean销毁时候执行的方法,这个接口有一个destroy方法,生命周期是是destroy----bean销毁---自定义的destroy方法
SimpleBean.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package ch5.destroy;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;public class SimpleBean implements InitializingBean,DisposableBean { public void afterPropertiesSet() throws Exception { System.out.println("this is info from afterpropertiesSet from SimpleBean"); }private String name; private String sex; private String age; public void destroyMethod(){ System.out.println("this is info from customer destroy method"); } public void destroy(){ System.out.println("this is info from destroy method"); }public String getAge() { return age;}public void setAge(String age) { this.age = age;}public String getName() { return name;}public void setName(String name) { this.name = name;}public String getSex() { return sex;}public void setSex(String sex) { this.sex = sex;}} |
配置文件:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?xml version="1.0" encoding="UTF-8"?><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.0.xsd"><bean id="SimpleBean" class="ch5.destroy.SimpleBean" destroy-method="destroyMethod"><property name="name"> <value>gaoxiang</value> </property><property name="sex"> <value>male</value></property><property name="age"> <value>26</value> </property></bean></beans> |
测试代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package ch5.destroy;import java.io.File;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.support.BeanDefinitionRegistry;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.FileSystemResource;public class TestSpring{ public static void main(String args[]) throws Exception{ //获取bean factory ConfigurableListableBeanFactory factory=(ConfigurableListableBeanFactory)getBeanFactory(); //使用子beanFactory SimpleBean bean1=(SimpleBean)factory.getBean("SimpleBean"); System.out.println("before destroy"); factory.destroySingletons(); System.out.println("after destory"); } public static BeanFactory getBeanFactory(){ //获取bean factory String realpath=""; //加载配置项 realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/destroy"+File.separator+"applicationContext.xml"; ConfigurableListableBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpath)); return factory; }} |
结果:
this is info from afterpropertiesSet from SimpleBean
before destroy
this is info from destroy method
this is info from customer destroy method
after destory
Spring回调方法DisposableBean接口的更多相关文章
- spring调用方法(接口和多个实现类的情况)
以spring框架注入bean说明接口TestService 有2个实现类 TestServiceImp1 @Service("TestService1") ,TestServic ...
- spring bean初始化及销毁你必须要掌握的回调方法
spring bean在初始化和销毁的时候我们可以触发一些自定义的回调操作. 初始化的时候实现的方法 1.通过java提供的@PostConstruct注解: 2.通过实现spring提供的Initi ...
- spring InitializingBean和DisposableBean init-method 和destroy-method @PostConstruct @PreDestroy
对于初始化函数: @PostConstruct 注解的方法 InitializingBean接口定义的回调afterPropertiesSet() Bean配置中自定义的初始化函数 对于析构则与上相同 ...
- Spring中Bean的生命中期与InitializingBean和DisposableBean接口
Spring提供了一些标志接口,用来改变BeanFactory中的bean的行为.它们包括InitializingBean和DisposableBean.实现这些接口将会导致BeanFactory调用 ...
- Spring钩子方法和钩子接口的使用详解
本文转自:http://www.sohu.com/a/166804449_714863 前言 SpringFramework其实具有很高的扩展性,只是很少人喜欢挖掘那些扩展点,而且官方的Refrenc ...
- Spring源码学习(六)-spring初始化回调方法源码学习
1.spring官方指定了三种初始化回调方法 1.1.@PostConstruct.@PreDestory 1.2.实现 InitializingBean DisposableBean 接口 1.3. ...
- Spring Bean生命周期回调方法
参阅官方文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory ...
- Spring bean 实现InitializingBean和DisposableBean接口实现初始化和销毁前操作
# InitializingBean接口> Spring Bean 实现这个接口,重写afterPropertiesSet方法,这样spring初始化完这个实体类后会调用这个方法```@Over ...
- fragment Activity之间传值的方法 之------------接口回调
首先 定义一个 回调接口 public interface FragmentCallBack { public void callbackFun1(Bundle arg); public void ...
随机推荐
- 【LintCode】计算两个数的交集(一)
问题分析: 既然返回值没有重复,我们不妨将结果放进set中,然后对两个set进行比较. 问题求解: public class Solution { /** * @param nums1 an inte ...
- an important difference between while and foreach on Perl
while (<STDIN>) { } # will read from standard input one line at a time foreach (<STDIN>) ...
- 【BZOJ-4289】Tax 最短路 + 技巧建图
4289: PA2012 Tax Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 168 Solved: 69[Submit][Status][Dis ...
- 【SDOI2010题集整合】BZOJ1922~1927&1941&1951&1952&1972&1974&1975
BZOJ1922大陆争霸 思路:带限制的单源最短路 限制每个点的条件有二,路程和最早能进入的时间,那么对两个值一起限制跑最短路,显然想要访问一个点最少满足max(dis,time) 那么每次把相连的点 ...
- [NOIP2013] 提高组 洛谷P1970 花匠
题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希 望剩下的花排列得比较别致. 具 ...
- raspberry pi的网络配置
这个像是interface的man. http://fts.ifac.cnr.it/cgi-bin/dwww?type=runman&location=interfaces/5 然后是一篇博文 ...
- Android登录界面实现
花了一些时间实现了一个还算可以等登陆界面,主要是对这两天工作的一个总结:自定义按钮.编辑框.布局.全屏等. 效果如下: 获取代码:点这里
- Objective-C NSData与实现NSCoding协议进行序列化和反序列化
1.NSData NSData是Objective-C语言中数据的基本类型,其成分可以理解为字节指针和长度的封装的类,来看看源代码 @interface NSData : NSObject <N ...
- jQuery—选择器
摘抄自<锋利的jQuery> 一.基本选择器 $("#one").css("background","#bbffaa"); 选取 ...
- codevs 1229 数字游戏(可重集的全排列)
传送门 Description Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间. 这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写某个数字),然后 ...