在Web应用程序中注册shutDownHook [英] Register shutDownHook in web application
问题描述
我们如何在Web应用程序中注册Shutdown hook?
How we can registerShutdown hook in web application?
在web.xml或applicationContext.xml中注册它有什么问题吗?
Is there any whays to register it in web.xml or in applicationContext.xml?
我知道如果我们使用主类的应用程序就很简单。
I know that if we are using application with main class it's simple.
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
context.registerShutdownHook();
但是网络应用呢?因为它在独立(非Web)应用程序中使用ContextListener
But what about web application? As it uses ContextListener
推荐答案
registerShutdownHook():
在bean方法上使用 @PreDestroy
注释,以便在从上下文中删除bean时或在上下文正在关闭。
The @PreDestroy
annotation is used on bean method to be notified when the bean is being removed from the context or when the context is shutting down.
context.close()
或调用context.registerShutdownHook()
。
@Component(value="someBean")
public class SomeBean {
@PreDestroy
public void destroy() {
System.out.println("Im inside destroy...");
}
}
我希望你已经知道这一点。
I hope you already know this.
registerShutdownHook():
在Web应用程序中,DispatcherServlet / ContextListener创建ApplicationContext,它将在服务器关闭时关闭上下文。您不需要显式调用 context.close()
或 context.registerShutdownHook()
。
In a web application, DispatcherServlet/ContextListener creates the ApplicationContext and it will close the context when the server shutdown. You don't need to explicitly invoke context.close()
or context.registerShutdownHook()
.
当服务器关闭时,bean上的 @PreDestory
方法将自动得到通知。
When the server shutdown, @PreDestory
methods on your bean will be notified automatically.
这篇关于在Web应用程序中注册shutDownHook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!