《Pro Spring》学习笔记之DestroySinglton()自动调用

来源:互联网 发布:淘宝首页优惠券代码 编辑:程序博客网 时间:2024/06/05 16:13

Spring中的destruction回调的唯一缺陷是无法自动触发,必须手动的 DestroySinglton()调用,如果我们使用的是servlet,大可以在servlet的destroy方法中进行调用,如果我们是单机应用程序,尤其是有多个退出点,就比较难办了,这是,我们可以为此作一个线程,加载到Runtime中,这样DestroySinglton()就会在程序结束时候自动调用了

 

ShutDownHook.java

 

package ch5.destroy;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class ShutDownHook implements Runnable {
    
    
private ConfigurableListableBeanFactory configurableListableBeanFactory;
    
public ShutDownHook(ConfigurableListableBeanFactory configurableListableBeanFactory){
        
this.configurableListableBeanFactory=configurableListableBeanFactory;
    }

    
public void run() {
        System.out.println(
"Destroy begin");
        configurableListableBeanFactory.destroySingletons();
        System.out.println(
"Destroy success");
    }


}

 

SimpleBean.java

 

package ch5.destroy;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class SimpleBean implements InitializingBean,DisposableBean {
  
public void afterPropertiesSet() throws Exception {
        System.out.println(
"this is info from afterpropertiesSet from SimpleBean");
    }

private  String name;
  
private String sex;
  
private String age;
  
public void destroyMethod(){
      System.out.println(
"this is info from customer destroy method");
  }

  
public void destroy(){
      System.out.println(
"this is info from destroy method");
  }

public String getAge() {
    
return age;
}

public void setAge(String age) {
    
this.age = age;
}

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;
}

}

 

测试代码:已经注释掉了显示调用destroySingletons的代码:

 

package ch5.destroy;

import java.io.File;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class TestSpring{
  
public static void main(String args[])  throws Exception{
      
//获取bean factory
      ConfigurableListableBeanFactory factory=(ConfigurableListableBeanFactory)getBeanFactory();  //使用子beanFactory
     
     
      SimpleBean bean1
=(SimpleBean)factory.getBean("SimpleBean");
      Runtime.getRuntime().addShutdownHook(
new Thread(new ShutDownHook(factory)));
//      System.out.println("before destroy");
//      factory.destroySingletons();
//      System.out.println("after destory");

  }

  
public static BeanFactory getBeanFactory(){
      
//获取bean factory
      String realpath="";
   
         
//加载配置项
      realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/destroy"+File.separator+"applicationContext.xml";
  
     
      ConfigurableListableBeanFactory  factory
=new XmlBeanFactory(new FileSystemResource(realpath));
      
      
return factory;
  }

}

 

运行结果:

this is info from afterpropertiesSet from SimpleBean
Destroy begin
this is info from destroy method
this is info from customer destroy method
Destroy success

发现是何显示调用一样的