在spring文档中有一批继承Aware接口,而Aware的作用是:

A marker superinterface indicating that a bean is eligible to be notified by the Spring container of a particular framework object through a callback-style method. The actual method signature is determined by individual subinterfaces but should typically consist of just one void-returning method that accepts a single argument.

一个标记超接口,指示bean有资格通过回调样式的方法被特定框架对象的Spring容器通知。实际的方法签名由单个子接口确定,但通常只应包含一个接受单个参数的void返回方法。

继承该接口的都有赋予提前暴露的意思,官方api中罗列出了所有继承该接口的接口

All Known Subinterfaces:

ApplicationContextAwareApplicationEventPublisherAwareBeanClassLoaderAwareBeanFactoryAwareBeanNameAwareBootstrapContextAwareEmbeddedValueResolverAwareEnvironmentAwareImportAwareLoadTimeWeaverAwareMessageSourceAwareNotificationPublisherAwareResourceLoaderAwareSchedulerContextAwareServletConfigAwareServletContextAware

 

下面我整理了些我们比较常用到的有:

1.BeanFactoryAware接口 暴露BeanFactory :实现该接口方法可以拿到暴露BeanFactory
2.ResourceLoaderAware接口 暴露ResourceLoader: 实现该接口获取资源加载器,可以获得外部资源文件
3.BeanNameAware接口 暴露beanName: 实现该接口可以获取bean名字
4.MessageSourceAware 暴露MessageSource:实现该接口可以设置国际化信息替换
5.EnvironmentAware 暴露Environment:获取所有配置信息继承PropertyResolver可以获取key-value
6.ApplicationContextAware 暴露ApplicationContext:实现该接口可以获取到容器上下文(ApplicationContext接口继承BeanFactory,包括Environment、MessageSource、ApplicationEventPublisher、ResourceLoader基本上涵盖以上所有作用)
7.ServletContextAware 暴露ServletContext:获取Servlet上下文信息
8.ServletConfigAware 暴露ServletConfig:包括可以获取ServletContext

 接着下面也是整理些在开发中比较常用的接口:

9.BeanPostProcessor接口  : bean实例化前后处理,实现该接口可以在bean实例化前后进行操作,与之类似接口BeanFactoryPostProcessor该接口在注册bean(BeanDefinitionsMap)后的操作
10.InitializingBean接口 :initializeBean时执行,即BeanPostProcessor执行完before后执行该接口方法afterPropertiesSet
11.ImportBeanDefinitionRegistrar接口: registerBeanDefinitions bean注册到BeanDefinitionsMap
12.ImportSelector接口,返回所有classname数组,在注册bean是调用获取,之后放入BeanDefinitionsMap与ImportBeanDefinitionRegistrar有点类似作用
13.DeferredImportSelector接口:继承ImportSelector接口,作用差不多,只不过该接口延迟被注册,ImportSelector接口注册完bean之后才会接着注册DeferredImportSelector的bean

14.ApplicationListener接口: 监听接口,当bean被初始化完成并被成功装载后会触发该事件,在最后一步finishRefresh时调用,实现ApplicationListener<ContextRefreshedEvent>接口可以收到监听动作
15.ApplicationContextInitializer接口: 刷新容器前也就是prepareContext时遍历调用,也就是bean注册前调用
16.FactoryBean接口:自定义Bean的创建过程,将对象放入beanFactory

 而以上这些接口什么时候被调用及初始化暴露的类呢?

下面我大概梳理下整个的流程:

BeanPostProcessor在实例化bean(一级缓存间)时被循环调用到,在该方法下initializeBean(finishBeanFactoryInitialization),先invokeAwareMethods这个方法进行调用BeanNameAware,BeanFactoryAware和BeanClassLoaderAware的重写方法进行赋值BeanName,BeanFactory和BeanClassLoader;然后接着遍历所有BeanPostProcessor接口的postProcessBeforeInitialization方法,其中ApplicationContextAwareProcessor类的postProcessBeforeInitialization方法是调用ApplicationContextAware,EnvironmentAware,ResourceLoaderAware、MessageSourceAware的重写方法进行赋值ApplicationContext,Environment,ResourceLoader、MessageSource,以及ServletContextAwareProcessor类的postProcessBeforeInitialization方法调用 ServletContextAware和ServletConfigAware的方法进行初始化 ServletContext和ServletConfig;初始化这些值之后,进行InitializingBean接口的afterPropertiesSet调用,然后接着遍历调用BeanPostProcessor接口的postProcessAfterInitialization。还有一个类似的接口InstantiationAwareBeanPostProcessor,该接口是在实例化对象放入beanFactory三级缓存中的前后进行调用操作和BeanPostProcessor类似(当然它继承于BeanPostProcessor),只不过比BeanPostProcessor接口要早被调用到