package org.springframework.beans.factory;
/**
* Interface to be implemented by beans that want to release resources on destruction.
* A {@link BeanFactory} will invoke the destroy method on individual destruction of a
* scoped bean. An {@link org.springframework.context.ApplicationContext} is supposed
* to dispose all of its singletons on shutdown, driven by the application lifecycle.
*
* <p>A Spring-managed bean may also implement Java's {@link AutoCloseable} interface
* for the same purpose. An alternative to implementing an interface is specifying a
* custom destroy method, for example in an XML bean definition. For a list of all
* bean lifecycle methods, see the {@link BeanFactory BeanFactory javadocs}.
*
* @author Juergen Hoeller
* @since 12.08.2003
* @see InitializingBean
* @see org.springframework.beans.factory.support.RootBeanDefinition#getDestroyMethodName()
* @see org.springframework.beans.factory.config.ConfigurableBeanFactory#destroySingletons()
* @see org.springframework.context.ConfigurableApplicationContext#close()
*/
public interface DisposableBean {
/**
* Invoked by the containing {@code BeanFactory} on destruction of a bean.
* @throws Exception in case of shutdown errors. Exceptions will get logged
* but not rethrown to allow other beans to release their resources as well.
*/
void destroy() throws Exception;
}
簡介
org.springframework.beans.factory.Aware
的一個子接口战转。
文檔
要在銷毀時釋放資源的 bean
所實現(xiàn)的接口共啃。BeanFactory
將在 有作用域的bean
單獨銷毀時調(diào)用 destroy
方法领舰。 一個 org.springframework.context.ApplicationContext
應(yīng)該在應(yīng)用程序生命周期的驅(qū)動下在關(guān)閉時處理其所有單例四敞。
出于同樣的目的甲棍,Spring
管理的bean
也可以實現(xiàn) Java
的 AutoCloseable
接口简识。 實現(xiàn)接口的替代方法是指定自定義的 destroy
方法,例如在 XML bean定義
中救军。
有關(guān)所有 bean生命周期
方法的列表财异,請參見 BeanFactory javadocs
。
destroy
由包含 BeanFactory
的銷毀 bean
調(diào)用唱遭。
@throws
在關(guān)閉錯誤的情況下發(fā)生異常戳寸。 異常將被記錄,但不會重新拋出以允許其他bean也釋放其資源拷泽。