在 Web 应用程序中注册 shutdownHook [英] Register shutDownHook in web application
问题描述
我们如何在网络应用程序中注册Shutdown钩子?
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();
但是网络应用程序呢?因为它使用 ContextListener
But what about web application? As it uses ContextListener
推荐答案
registerShutdownHook() 在独立(非网络)应用程序中:
@PreDestroy
注释用于 bean 方法,以便在 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()
被调用时触发.
Shut down event is fired when context.close()
or context.registerShutdownHook()
is invoked.
@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屋!