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.
 *
 * 

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. *

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 在配置错误(例如无法设置基本属性)或初始化由于任何其他原因而失败的情况下发生的异常。

你可能感兴趣的:(org.springframework.beans.factory.InitializingBean)