Executing a Method When a Bean Is Destroyed

来源:互联网 发布:slax linux中文版 编辑:程序博客网 时间:2024/06/05 23:50
  1. Using JSR-250 @PreDestroy Annotation
  2. Implementing the DisposableBean Interface
  3. specify the name of the methodin the destroy-method attribute of the bean’s <bean> tag.

Spring calls it just before it destroys the singleton   instance of the bean (as stated before, Spring will not call this method for those beans with prototype

scope).


The only drawback of the destruction callbacks in Spring is that they are not fired automatically; you need
to remember to call AbstractApplicationContext.destroy() before your application is closed.

AbstractApplicationContext’s registerShutdownHook() automatically instructs
Spring to register a shutdown hook of the underlying JVM runtime.