Spring源码之Aware接口执行过程

前言:在Spring中存在一种后缀为Aware的接口,可以理解为感知,也就是说让Bean实例感知一些与自身相关的东西,如自己的BeanName,加载自己的ClassLoader等等;本文下面将介绍一些常见的Aware接口,其介绍顺序为该接口在Spring中的执行顺序;

1. BeanNameAware

public interface BeanNameAware extends Aware {

	/**
	 * 感知Bean实例自身的BeanName
	 * 在Bean生命周期中的初始化阶段执行
	 */
	void setBeanName(String name);

}

2. BeanClassLoaderAware

public interface BeanClassLoaderAware extends Aware {

	/**
	 * 感知Bean实例自身的ClassLoader
	 * 在Bean生命周期中的初始化阶段执行
	 */
	void setBeanClassLoader(ClassLoader classLoader);

}

3. BeanFactoryAware

public interface BeanFactoryAware extends Aware {

	/**
	 * 感知Bean实例自身所在的BeanFactory
	 * 在Bean生命周期中的初始化阶段执行
	 */
	void setBeanFactory(BeanFactory beanFactory) throws BeansException;

}

4. EnvironmentAware

public interface EnvironmentAware extends Aware {

	/**
	 * 感知当前的环境
	 * 在Bean生命周期中的初始化阶段被ApplicationContextAwareProcessor调用postProcessBeforeInitialization方法执行
	 * Environment: 具有判断当前环境和提供properties属性访问的能力
	 */
	void setEnvironment(Environment environment);

}

5. ResourceLoaderAware

public interface ResourceLoaderAware extends Aware {

	/**
	 * 感知资源访问器
	 * 在Bean生命周期中的初始化阶段被ApplicationContextAwareProcessor调用postProcessBeforeInitialization方法执行
	 * ResourceLoader: 具有访问本地、classpath、WEB-INF目录下资源的能力
	 */
	void setResourceLoader(ResourceLoader resourceLoader);

}

6. ApplicationEventPublisherAware

public interface ApplicationEventPublisherAware extends Aware {

	/**
	 * 感知应用的事件发布器
	 * 在Bean生命周期中的初始化阶段被ApplicationContextAwareProcessor调用postProcessBeforeInitialization方法执行
	 * ApplicationEventPublisher: 具有发布事件的能力
	 */
	void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);

}

7. MessageSourceAware

public interface MessageSourceAware extends Aware {

	/**
	 * 感知支持国际化消息解析器
	 * 在Bean生命周期中的初始化阶段被ApplicationContextAwareProcessor调用postProcessBeforeInitialization方法执行
	 * MessageSource: 具有支持国际化的消息解析的能力
	 */
	void setMessageSource(MessageSource messageSource);

}

8. ApplicationContextAware

public interface ApplicationContextAware extends Aware {

	/**
	 * 感知Spring应用上下文
	 * 在Bean生命周期中的初始化阶段被ApplicationContextAwareProcessor调用postProcessBeforeInitialization方法执行
	 */
	void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JackieGGu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值