下面来介绍一下Spring种Bean的销毁。销毁的方法和初时化的方法非常的类似:
完成Bean的销毁也有两种方式;
1在配置文件种指定destroy-method属性来完成
2Bean类实现org.springframework.beans.factory.DsiposableBean
下面看一下代码
package com.jnotnull;
public class HelloWorld {
public String message = null;
public String cleanup() {
this.message="";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id ="HelloWorld" class="com.jnotnull.HelloWorld" destory-method="cleanup">
<bean>
</beans>
第二种,实现DisposableBean接口
package com.jnotnull;
import org.springframework.beans.factory.DisposableBean;
public class HelloWorld implements DisposableBean{
public String message = null;
public void destroy(){
this.message = "";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
完成Bean的销毁也有两种方式;
1在配置文件种指定destroy-method属性来完成
2Bean类实现org.springframework.beans.factory.DsiposableBean
下面看一下代码
package com.jnotnull;
public class HelloWorld {
public String message = null;
public String cleanup() {
this.message="";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id ="HelloWorld" class="com.jnotnull.HelloWorld" destory-method="cleanup">
<bean>
</beans>
第二种,实现DisposableBean接口
package com.jnotnull;
import org.springframework.beans.factory.DisposableBean;
public class HelloWorld implements DisposableBean{
public String message = null;
public void destroy(){
this.message = "";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}