I am implementing the DisposableBean in one of my beans to get notified when my server is shutting down. However, the destroy method is never called.
I am running Tomcat with S开发者_C百科pring 3.
Any suggestions?
Use ContextLoaderListener and override contextDestroyed
method.
Don't forget to declare ContextLoaderListener
in your web.xml
.
Instead of using ApplicationContext, use AbstractApplicationContext. Then register the same. Spring wont call DisposableBean for desktop application if you are using ApplicationContext.
Ex: AbstractApplicationContext appCxt= new ClassPathXmlApplicationContext("spring.xml"); appCxt.registerShutdownHook();
I tried this option.
With obj
object with destroy()
method, add this:
Runtime.getRuntime ( ).addShutdownHook ( new Thread ( obj::destroy ) );
精彩评论