291
技術社區[雲棲]
Spring-Bean的銷毀使用destroy-method()方法無效解決方案(容器!附源碼)
Bean
package com.gc.action;
import java.util.Date;
public class HelloWorld {
private String msg=null;//該變量用來存儲字符串
private Date date=null;//該變量用來存儲日期
// public HelloWorld(String msg)
// {
// this.msg=msg;
// }
//
// public HelloWorld()//這個必須寫,否則不能轉到上麵的那個
// {
// this.msg=msg;
// }
//初始化
public void init(){
this.msg="HelloWorld";
this.date=new Date();
}
//銷毀
public void cleanup(){
this.msg="";
this.date=null;
System.out.println("您銷毀了msg"+this.msg+"和date"+this.date);
}
//設定變量msg的set方法
public void setMsg(String msg) {
this.msg=msg;
}
//獲取變量msg的get方法
public String getMsg() {
return this.msg;
}
public Date getDate() {
return this.date;
}
public void setDate(Date date) {
this.date = date;
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"https://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--定義一個Bean-->
<bean init-method="init" destroy-method="cleanup">
<!--將其變量msg通過依賴注入-->
</bean>
</beans>
經過修改,原來是容器的問題!容器需要關閉是前提條件,SPRING BEAN的生命周期,SPRING的單例模式,動態代理,反射機製,一定要懂得。寫中間件的經曆就會特別有助於理解這些框架性的東西:
修改後的項目地址如下:https://download.csdn.net/detail/opzoonzhuzhengke/4156763
https://download.csdn.net/detail/opzoonzhuzhengke/4156763
修改後的代碼:
BEAN:
package com.gc.action;
import java.util.Date;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.DisposableBean;
public class HelloWorld
//implements InitializingBean,DisposableBean
{
private String msg=null;//該變量用來存儲字符串
private Date date=null;//該變量用來存儲日期
public void afterPropertiesSet() {
// TODO Auto-generated method stub
this.msg="HelloWorld";
this.date=new Date();
System.out.println("2000");
}
public void cleanup() {
// TODO Auto-generated method stub
this.msg="";
this.date=null;
System.out.println("您銷毀了msg"+this.msg+"和date"+this.date);
}
public HelloWorld(String msg)
{
this.msg=msg;
}
public HelloWorld()//這個必須寫,否則不能轉到上麵的那個
{
this.msg=msg;
}
//設定變量msg的set方法
public void setMsg(String msg) {
this.msg=msg;
}
//獲取變量msg的get方法
public String getMsg() {
return this.msg;
}
public Date getDate() {
return this.date;
}
public void setDate(Date date) {
this.date = date;
}
}
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"https://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--定義一個Bean-->
<bean init-method="afterPropertiesSet" destroy-method="cleanup">
<!--將其變量msg通過依賴注入-->
</bean>
</beans>
測試程序:
package com.gc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.gc.action.HelloWorld;
public class TestHelloWorld {
public static void main(String[] args)
{
//通過ApplicationContext來獲取Spring文件的配置
AbstractApplicationContext actx=new ClassPathXmlApplicationContext("config.xml");
//通過Bean的id來獲取Bean
HelloWorld helloworld=(HelloWorld)actx.getBean("HelloWorld");
//打印輸出
System.out.println(helloworld.getMsg()+" "+helloworld.getDate());
actx.close();
}
}
最重要的輸出部分:
2000
HelloWorld Tue Mar 20 15:36:46 CST 2012
您銷毀了msg和datenull
最後更新:2017-04-02 22:16:38