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





















SimpleBean.java
















































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








































运行结果:
this is info from afterpropertiesSet from SimpleBean
Destroy begin
this is info from destroy method
this is info from customer destroy method
Destroy success
发现是何显示调用一样的