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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值