上一篇我们讲到了InitializingBean,这一篇我们看下DisposableBean接口,它也只提供一个方法destroy()。如果实现了DisposebleBean接口,那么Spring将自动调用bean中的Destory方法进行销毁,下面我们看下DisposebleBean应用:
package com.ck.bean;
import java.io.Serializable;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Person implements InitializingBean, DisposableBean, Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String sex;
private Integer age;
public Person() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Person InitializingBean afterPropertiesSet");
}
//注意这个方法
@Override
public void destroy() throws Exception {
System.out.println("Person DisposableBean destroy");
}
}
看下配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<bean id="person" class="com.ck.bean.Person"/>
</beans>
看下测试代码:
package com.ck.ioc;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class IocTest {
@Test
public void test() {
ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext( "src/main/resources/applicationContext.xml");
//注意这个方法
ctx.close();
}
}
测试结果:
四月 03, 2019 8:19:03 下午 org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@3c679bde: startup date [Wed Apr 03 20:19:03 CST 2019]; root of context hierarchy
四月 03, 2019 8:19:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [X:\CK\stsws\springWeb\src\main\resources\applicationContext.xml]
Person InitializingBean afterPropertiesSet
四月 03, 2019 8:19:04 下午 org.springframework.context.support.FileSystemXmlApplicationContext doClose
信息: Closing org.springframework.context.support.FileSystemXmlApplicationContext@3c679bde: startup date [Wed Apr 03 20:19:03 CST 2019]; root of context hierarchy
Person DisposableBean destroy
我们看到destroy方法被执行.
小结:DisposableBean主要用于我们销毁bean的时候做一些逻辑处理.