1. 程式人生 > >Spring-Bean的銷燬使用destroy-method()方法無效解決方案(容器!附原始碼)

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"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!--定義一個Bean-->
    <bean id="HelloWorld" class="com.gc.action.HelloWorld" init-method="init" destroy-method="cleanup">
    <!--將其變數msg通過依賴注入-->
 

    
    </bean>
    

</beans>


經過修改,原來是容器的問題!容器需要關閉是前提條件,SPRING BEAN的生命週期,SPRING的單例模式,動態代理,反射機制,一定要懂得。寫中介軟體的經歷就會特別有助於理解這些框架性的東西:

修改後的專案地址如下:http://download.csdn.net/detail/opzoonzhuzhengke/4156763

http://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"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!--定義一個Bean-->
    <bean id="HelloWorld" class="com.gc.action.HelloWorld" 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