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 的一個(gè)子接口。
文檔
要在銷毀時(shí)釋放資源的 bean 所實(shí)現(xiàn)的接口。BeanFactory 將在 有作用域的bean 單獨(dú)銷毀時(shí)調(diào)用 destroy 方法。 一個(gè) org.springframework.context.ApplicationContext 應(yīng)該在應(yīng)用程序生命周期的驅(qū)動(dòng)下在關(guān)閉時(shí)處理其所有單例。
出于同樣的目的,Spring 管理的bean 也可以實(shí)現(xiàn) Java 的 AutoCloseable 接口。 實(shí)現(xiàn)接口的替代方法是指定自定義的 destroy 方法,例如在 XML bean定義 中。
有關(guān)所有 bean生命周期 方法的列表,請(qǐng)參見 BeanFactory javadocs。
destroy
由包含 BeanFactory 的銷毀 bean 調(diào)用。
@throws 在關(guān)閉錯(cuò)誤的情況下發(fā)生異常。 異常將被記錄,但不會(huì)重新拋出以允許其他bean也釋放其資源。