Java – register a close hook in spring 2.5

I have a spring application that does not call the bean destroy method when shutdown I have seen that this is due to instantiation in beanreffactory and can be avoided by manually calling registershutdownhook () in the application context This method seems to have disappeared from the spring between 2.0 and 2.5

Can anyone point out where I'm going now?

thank you.

Solution

This method is still available in configurableapplicationcontext and is implemented by abstractapplicationcontext

So you can do this

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

Alternatively, you can simply call ((configurableapplicationcontext) CTX) when you close the application or use your own close hook close()

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run(){
       if (ctx instanceof ConfigurableApplicationContext) {
           ((ConfigurableApplicationContext)ctx).close();
       }
    }
 });
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>