org.springframework.beans.factory.DisposableBean

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 所实现的接口。BeanFactory 将在 有作用域的bean 单独销毁时调用 destroy 方法。 一个 org.springframework.context.ApplicationContext 应该在应用程序生命周期的驱动下在关闭时处理其所有单例。
出于同样的目的,Spring 管理的bean 也可以实现 Java 的 AutoCloseable 接口。 实现接口的替代方法是指定自定义的 destroy 方法,例如在 XML bean定义 中。

有关所有 bean生命周期 方法的列表,请参见 BeanFactory javadocs。

destroy

由包含 BeanFactory 的销毁 bean 调用。
@throws 在关闭错误的情况下发生异常。 异常将被记录,但不会重新抛出以允许其他bean也释放其资源。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 2.1 我们的理念是:让别人为你服务 IoC是随着近年来轻量级容器(Lightweight Container)的...
    好好学习Sun阅读 2,918评论 0赞 11
  • 25个经典的Spring面试问答 本人收集了一些在大家在面试时被经常问及的关于Spring的主要问题,这些问题有可...
    杀小贼阅读 794评论 0赞 2
  • 说明:本文主要内容来自慕课网。配合视频食用口味更佳。主要是顺着已经学习的视频顺序总结一遍,以深化理解和方便日后复习...
    stoneyang94阅读 931评论 3赞 5
  • 参考W3C Spring教程 Spring致力于J2EE应用的各种解决方案,而不仅仅专注于某一层解决方案。可以说S...
    王侦阅读 1,263评论 0赞 6
  • 这世间所有最深的真情,都源自懂得。有缘相知,真心相伴,灵魂便有了交集。 懂得,是心灵的交汇,是一种心语;相知的两个...
    longouxiangai阅读 427评论 1赞 0

友情链接更多精彩内容