在Spring 2.5中注册一个关机钩子 [英] Registering a shutdown hook in Spring 2.5

查看:85
本文介绍了在Spring 2.5中注册一个关机钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个spring应用程序,该应用程序在关机时不调用bean destroy方法.我已经看到了对此的引用是由于在beanRefFactory中的实例化,并且可以通过在应用程序上下文中手动调用registerShutdownHook()来避免这种情况.该方法在Spring 2.0-2.5版本之间似乎已经消失了.

I have a spring application which is not calling bean destroy methods on shutdown. I've seen references to this being due to instantiation in a beanRefFactory, and that this can be circumvented through manually calling registerShutdownHook() on an the application context.This method seems to have disappeared from spring between versions 2.0 - 2.5.

有人可以指出我现在的方向吗?

Can someone point me in the direction of how this is now done?

谢谢.

推荐答案

所以您也许可以做到这一点

So you might be able to do this

ApplicationContext ctx = ...;
if (ctx instanceof ConfigurableApplicationContext) {
    ((ConfigurableApplicationContext)ctx).registerShutdownHook();
}

或者,您可以在关闭应用程序或使用自己的关闭钩子时直接调用((ConfigurableApplicationContext)ctx).close():

Alternatively, you could simply call ((ConfigurableApplicationContext)ctx).close() yourself while closing down the application or using your own shutdown hook:

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run(){
       if (ctx instanceof ConfigurableApplicationContext) {
           ((ConfigurableApplicationContext)ctx).close();
       }
    }
 });

这篇关于在Spring 2.5中注册一个关机钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆