org.springframework.beans.factory.InitializingBean

package org.springframework.beans.factory;

/**
 * Interface to be implemented by beans that need to react once all their properties
 * have been set by a {@link BeanFactory}: e.g. to perform custom initialization,
 * or merely to check that all mandatory properties have been set.
 *
 * <p>An alternative to implementing {@code InitializingBean} is specifying a custom
 * init method, for example in an XML bean definition. For a list of all bean
 * lifecycle methods, see the {@link BeanFactory BeanFactory javadocs}.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @see DisposableBean
 * @see org.springframework.beans.factory.config.BeanDefinition#getPropertyValues()
 * @see org.springframework.beans.factory.support.AbstractBeanDefinition#getInitMethodName()
 */
public interface InitializingBean {

    /**
     * Invoked by the containing {@code BeanFactory} after it has set all bean properties
     * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
     * <p>This method allows the bean instance to perform validation of its overall
     * configuration and final initialization when all bean properties have been set.
     * @throws Exception in the event of misconfiguration (such as failure to set an
     * essential property) or if initialization fails for any other reason
     */
    void afterPropertiesSet() throws Exception;

}

简介

org.springframework.beans.factory.Aware 的一个子接口。

文档

BeanFactory 设置完所有属性后需要立即响应的bean所实现的接口:执行自定义初始化,或仅检查是否已设置所有必填属性。

实现 InitializingBean 的另一种方法是指定自定义 init 方法,例如在 XML bean 定义中。

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

afterPropertiesSet

由包含的 BeanFactory 设置了所有 bean属性 并满足 BeanFactoryAwareApplicationContextAware 等之后调用。
设置所有 bean属性 后,此方法允许 bean实例 执行其整体配置的验证和最终初始化。
@throws 在配置错误(例如无法设置基本属性)或初始化由于任何其他原因而失败的情况下发生的异常。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 参考W3C Spring教程 Spring致力于J2EE应用的各种解决方案,而不仅仅专注于某一层解决方案。可以说S...
    王侦阅读 1,186评论 0 6
  • BeanFactory总览 BeanFactory接口是Spring IoC的基础。它的特定契约主要用于与Spri...
    WILL_HUNTING阅读 3,066评论 0 1
  • 一、Spring IOC其实很简单  有部分Java开发者对IOC(Inversion Of Control)和D...
    Chandler_珏瑜阅读 4,504评论 0 41
  • Spring入门使用Spring容器Spring容器使用ApplicationContextApplication...
    渐丶忘阅读 1,423评论 0 4
  • 培养孩子的社会能力这一本书,其实整本书都在围绕字词游戏这个工具。考虑后果的字词,有新的是之前和之后,可以通过之前和...
    我只想静静的_1a1b阅读 455评论 0 4