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