How we can registerShutdown hook in web application?
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();
But what about web application? As it uses ContextListener
registerShutdownHook() in standalone (non-web) application:
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.
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() in web application:
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()
.
When the server shutdown, @PreDestory
methods on your bean will be notified automatically.