
前言: 最近工作中正好使用的感知接口, 简单整理相关的知识。因为感知接口有哪些Spring框架是如何实现的也是常见的面试问题
目录
2.6ApplicationEventPublisherAware接口
Spring内置了用于不同目的的大量感知接口,例如:实例BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实例ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。
Spring Aware的目的就是为了让Bean获得Spring容器的服务,感知接口都继承Aware接口。Aware接口中没有实现方法,它只是一个标记超接口。它表明实现此接口的Spring容器中的bean ,在初始化时候可以实现某个回调样方法,实际回调的的方法签名由各个子接口确定
- package org.springframework.beans.factory
-
- public interface Aware {
- //标记接口
- }

该接口回调接口将ClassLoader对象注入到时受管Bean的实例中可让受管Bean本身知道它是由哪一类装载器负责装载的
- public interface BeanClassLoaderAware extends Aware {
-
- void setBeanClassLoader(ClassLoader classLoader);
- }
该接口将对象ApplicationContextAware对象注入到当前受管Bean实例中,这样受管Bean能够感知到IoC容器的存在并得到相应的支持
- public interface ApplicationContextAware extends Aware {
-
- /**
- *设置运行此对象的ApplicationContext。通常此调用将用于初始化对象。
- *在填充普通bean属性之后但在初始化回调之前调用
- */
- void setApplicationContext(ApplicationContext applicationContext)
- throws BeansException;
- }
该接口将对象BeanFactory对象注入到受管Bean实例中,
- public interface BeanFactoryAware extends Aware {
-
- /*
- *向bean实例提供所属工厂的回调。
- *在填充正常bean属性后调用但在初始化回调之前
- */
- void setBeanFactory(BeanFactory beanFactory) throws BeansException;
- }
该接口将Bean本身的id(name)对象注入到受管Bean实例中,
- public interface BeanNameAware extends Aware {
-
- /*
- *在创建此bean的bean工厂中设置bean的名称。
- *在填充普通bean属性之后但在初始化之前调用
- *类似InitializingBean的afterPropertiesSet或自定义init方法的回调
- */
- void setBeanName(String name);
- }
该接口负责将MessageSource对象注入到当前受管Bean实例中,这样受管Bean能够获得国际化和本地化消息的支持
- //请注意,MessageSource通常也可以作为bean传递引用(任意bean属性或构造函数参数)
- //因为它在应用程序上下文中定义为名为“messageSource”的bean
- public interface MessageSourceAware extends Aware {
-
- /*
- *设置运行此对象的MessageSource。
- *在填充普通bean属性之后但在初始化之前调用
- */
- void setMessageSource(MessageSource messageSource);
- }
该接口将ApplicationEventPublisher对象注入到当前受管Bean实例中,这样受理的Bean能够获得分发事件的支持
- public interface ApplicationEventPublisherAware extends Aware {
- /*
- *设置运行此对象的ApplicationEventPublisher
- *在填充普通bean属性之后但在初始化之前调用
- *此对象要使用的@param applicationEventPublisher事件发布器
- */
- void setApplicationEventPublisher(ApplicationEventPublisher
- applicationEventPublisher);
- }
该接口接口负责将ResourceLoader对象注入到当前的受管Bean实例中,这样受管Bean能够得到获取资源的支持
- public interface ResourceLoaderAware extends Aware {
- /*
- * 设置运行此对象的ResourceLoader
- */
- void setResourceLoader(ResourceLoader resourceLoader);
- }
先看下实例化bean对象的过程,大致如下图,随着Spring版本提升细节虽然有变化但是大致流程相同。

(图片来自网络)
给BEAN实例注入所需资源是在后置处理阶段发生, ApplicationContextAwareProcessor 实现接口BeanPostProcessor ,在初始化之前调用invokeAwareInterfaces方法实现将受管BEAN所需要的资源注入到BEAN对象中
源代码如下:
- class ApplicationContextAwareProcessor implements BeanPostProcessor {
-
- //代码略
-
- //接口BeanPostProcessor的方法实现
- //在实例化及依赖注入完成后、在任何初始化代码(比如配置文件中的init-method)调用之前调用
- @Override
- @Nullable
- public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
- if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
- bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
- bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware ||
- bean instanceof ApplicationStartupAware)) {
- return bean;
- }
-
- AccessControlContext acc = null;
-
- if (System.getSecurityManager() != null) {
- acc = this.applicationContext.getBeanFactory().getAccessControlContext();
- }
-
- if (acc != null) {
- AccessController.doPrivileged((PrivilegedAction
- invokeAwareInterfaces(bean);
- return null;
- }, acc);
- }
- else {
- invokeAwareInterfaces(bean);
- }
-
- return bean;
- }
-
- // 实例化BEAN初始化之前,将所需的资源注入到BEAN中
- private void invokeAwareInterfaces(Object bean) {
- if (bean instanceof EnvironmentAware) {
- ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
- }
- if (bean instanceof EmbeddedValueResolverAware) {
- ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
- }
- if (bean instanceof ResourceLoaderAware) {
- ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
- }
- if (bean instanceof ApplicationEventPublisherAware) {
- ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
- }
- if (bean instanceof MessageSourceAware) {
- ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
- }
- if (bean instanceof ApplicationStartupAware) {
- ((ApplicationStartupAware) bean).setApplicationStartup(this.applicationContext.getApplicationStartup());
- }
- if (bean instanceof ApplicationContextAware) {
- ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
- }
- }
-
- }
综上所述,实现感知接口Aware的Bean在被初始之后,DI容器就会自动调用这些回调接口所定义的方法,进而将相关对象注入进来,以取得所需的资源。
创建一个SpringBoot工程,但是本用例不使用Application启动,创建一个配置类如下,配置中包含一个Bean
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- public class ConfigC {
-
- @Bean
- public AwareTestService awareTestService() {
- return new AwareTestService();
- }
- }
用于测试设置AwareTestService 实现接口 BeanNameAware, BeanClassLoaderAware,当然也可以实现其他的接口来测试
- import org.springframework.beans.factory.BeanClassLoaderAware;
- import org.springframework.beans.factory.BeanNameAware;
- import org.springframework.stereotype.Service;
-
- @Service
- public class AwareTestService implements BeanNameAware, BeanClassLoaderAware {
-
- private ClassLoader classLoader;
- private String beanName;
-
- @Override
- public void setBeanClassLoader(ClassLoader classLoader) {
- this.classLoader=classLoader;
- }
-
- @Override
- public void setBeanName(String name) {
- this.beanName=name;
- }
-
- public void getInfo(){
- System.out.println(beanName);
- System.out.println(classLoader.toString());
- }
- }
创建启动类用于测试,通过加载ConfigC来启动容器,并通过getBean方法打印Bean信息
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-
- public class AwareTest {
- public static void main(String[] args) {
- ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigC.class);
- AwareTestService b = ctx.getBean(AwareTestService.class);
- b.getInfo();
- }
- }
运行结果如下
- 15:08:17.668 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@768b970c
- 15:08:17.681 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
- 15:08:17.777 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
- 15:08:17.779 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
- 15:08:17.780 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
- 15:08:17.781 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
- 15:08:17.788 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'configC'
- 15:08:17.792 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'awareTestService'
- awareTestService
- jdk.internal.loader.ClassLoaders$AppClassLoader@2437c6dc