开发者

Spring DisposableBean does not work

开发者 https://www.devze.com 2023-02-16 12:34 出处:网络
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 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 ) );

0

精彩评论

暂无评论...
验证码 换一张
取 消