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

本文介绍如何在Spring框架中实现bean销毁回调的自动触发,通过注册一个关闭钩子来确保在应用结束时能够调用销毁方法,从而释放资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

发现是何显示调用一样的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值