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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…