博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring回调方法DisposableBean接口
阅读量:4630 次
发布时间:2019-06-09

本文共 3148 字,大约阅读时间需要 10 分钟。

除了自定义的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

 原文地址:

转载于:https://www.cnblogs.com/langtianya/p/5013213.html

你可能感兴趣的文章
JavaScript获取当前日期,昨天,今天日期以及任意天数间隔日期
查看>>
电子宠物系统
查看>>
windows远程桌面如果超出最大连接数, 使用命令行mstsc /console登录即可
查看>>
49. Group Anagrams
查看>>
SPOJ ATOMS - Atoms in the Lab
查看>>
关于 ListBox 自动换行
查看>>
postman测试上传文件
查看>>
R. ftp软件
查看>>
List<T>中,Remove和RemoveAt区别
查看>>
十月回家记
查看>>
ZOJ 3735 dp
查看>>
android效果背景虚化
查看>>
jQuery效果:隐藏、显示、切换、滑动、淡入淡出、动画
查看>>
Java 学习笔记(4)——java 常见类
查看>>
IOS开源项目汇总
查看>>
用herl工具解决微信内链接或二维码可直接用外部浏览器打开
查看>>
GITHup的使用
查看>>
void main()是错的!
查看>>
Atitit. Attilax企业框架 AEF的发展里程总结
查看>>
亚麻 面经_ml
查看>>