spring暴露接口及解释(一)

一. InitializingBean接口

  1. 实现该接口,在bean生成后回调afterPropertiesSet()方法;可用于执行自定义初始化,或者检查是否设置了所有{强制属性}。
  2. 该机制会在所有bean实例化并设置好bean属性后,执行最终配置或初始化
package org.springframework.beans.factory;

public interface InitializingBean {

	/**
	*  1.在设置了所有bean属性后由包含{@code BeanFactory}调用
	*  满足{@link BeanFactoryAware}, {@code applicationcontext taware}等。
	 * Invoked by the containing {@code BeanFactory} after it has set all bean properties
	 * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
	 *  
	 * 2. 该方法允许bean实例执行其整体验证
	 *  设置好所有bean属性后,进行配置和最终初始化。
	 * <p>This method allows the bean instance to perform validation of its overall
	 * configuration and final initialization when all bean properties have been set.
	 *  
	 * 3.在配置错误的情况下抛出异常(例如未能设置
	 *  essential属性)或初始化失败的任何其他原因。
	 * @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;
}

二. ApplicationContextAware接口

  1. 实现该接口,获得它所在执行的ApplicationContext对象,可用来初始化object
  2. 实现此接口{查询bean}的效率仅仅比{配置通过bean引用}慢一点
  3. 该接口回调在{InitializingBean#afterPropertiesSet()或自定义配置}之前,在{ResourceLoaderAware#setResourceLoader、
    ApplicationEventPublisherAware#setApplicationEventPublisher、
    MessageSourceAware
    }之后
package org.springframework.context;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;

public interface ApplicationContextAware extends Aware {

	/**
	 * <p>Invoked after population of normal bean properties but before an init callback such
	 * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
	 * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
	 * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
	 * {@link MessageSourceAware}, if applicable.
	 * @param applicationContext the ApplicationContext object to be used by this object
	 * @throws ApplicationContextException in case of context initialization errors
	 * @throws BeansException if thrown by application context methods
	 * @see org.springframework.beans.factory.BeanInitializationException
	 */
	void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

三. ApplicationEventPublisherAware接口

  1. 实现该接口,setApplicationEventPublisher()获得它所在执行的ApplicationEventPublisher
  2. 此接口回调在填充普通bean属性后,在{
    InitializingBean’s afterPropertiesSet、
    自定义初始化、
    ApplicationContextAware’s setApplicationContext
    }之前
 package org.springframework.context;

import org.springframework.beans.factory.Aware;

public interface ApplicationEventPublisherAware extends Aware {

	/**
	 * Set the ApplicationEventPublisher that this object runs in.
	 * <p>Invoked after population of normal bean properties but before an init
	 * callback like InitializingBean's afterPropertiesSet or a custom init-method.
	 * Invoked before ApplicationContextAware's setApplicationContext.
	 * @param applicationEventPublisher event publisher to be used by this object
	 */
	void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);

}
  • 这三个接口回调执行顺序
    ApplicationEventPublisherAware ->
    ApplicationContextAware ->
    InitializingBean

猜你喜欢

转载自blog.csdn.net/weixin_43792738/article/details/106601729