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 ...
随机推荐
- js-JavaScript高级程序设计学习笔记11
依然第十四章 1.选择部分文本:使用setSelectionRange()方法,两个参数为第一个字符的索引和最后一个字符之后的索引,类似于substring()方法. 2.IE8及更早版本使用范围选择 ...
- 【BZOJ-4591】超能粒子炮·改 数论 + 组合数 + Lucas定理
4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 95 Solved: 33[Submit][Statu ...
- 【BZOJ-1026】windy数 数位DP
1026: [SCOI2009]windy数 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5230 Solved: 2353[Submit][Sta ...
- bzoj2819 Nim
题意:给定一棵带点权的树,每次询问用一条路径上的点玩Nim游戏先手是否必胜,支持单点修改. Nim游戏:所有堆的数目异或起来不为0时先手必胜,否则必败. 所以就是单点修改+路径异或和查询. 树剖一发, ...
- C#把数据写到硬盘指定位置
FileStream fs; StreamWriter sw;string RootPath = @"C:\Advantech" + "\\tempData\\" ...
- Simultaneous Tag Editing in IntelliJ IDEA 14.1
If you're involved in web development and, for some reason, you haven't given a ride to IntelliJ IDE ...
- (转载)最长递增子序列 O(NlogN)算法
原博文:传送门 最长递增子序列(Longest Increasing Subsequence) 下面我们简记为 LIS. 定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则 ...
- PMD使用提醒
1.注册登录后进入主界面,向右滑动出现侧滑栏 2.点击蓄水球进行每日目标设定 3.设置计时失败发送朋友圈内容 ~~~~~开始体验吧!!!
- UML 简单介绍
Unified modeling Language - 统一建模语言
- JavaWeb---总结(十二)Session
一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...