在 Spring 框架中有一个 org.springframework.beans.factory.Aware 接口,

Aware 是感知感应的意思,那么此接口的作用就是为 Spring 中的 bean 提供了感知外界的能力。

Aware接口本身只是一个标记接口,Spring 中提供了一系列具有具体感知能力的接口,它们都继承自 Aware 接口,

  部分如下:

  LoadTimeWeaverAware:加载 Spring Bean 时织入第三方模块,如AspectJ

  BeanClassLoaderAware:加载 Spring Bean 的类加载器

  BootstrapContextAware:资源适配器 BootstrapContext,如JCA,CCI

  ResourceLoaderAware:底层访问资源的加载器

  BeanFactoryAware:声明 BeanFactory

  PortletConfigAware:PortletConfig

  PortletContextAware:PortletContext

  ServletConfigAware:ServletConfig

  ServletContextAware:ServletContext

  MessageSourceAware:国际化

  ApplicationEventPublisherAware:应用事件

  NotificationPublisherAware:JMX通知

  BeanNameAware:声明 Spring Bean 的名字

  此处以 BeanNameAware 举例,其内部包含了一个 setBeanName 方法,所以当一个 bean 继承了此接口,并定义一个 beanName 的属性,

则在 Spring 容器初始化这个 bean 的时候,会调用这个 setBeanName 方法,注入这个 beanName。

  不同的 Aware 注入对应的外界信息的时机是不一样的,具体也分为两类:

  1. BeanNameAware/BeanClassLoaderAware/BeanFactoryAware

  这三类 Aware 生效的时机是在容器创建 bean 的最后一步完成,具体的调用顺序如下:

  getBean(String beanName)

    doGetBean(final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)

      getSingleton(String beanName, ObjectFactory<?> singletonFactory)

  在 getSingleton 会回调

  createBean(String beanName, RootBeanDefinition mbd, Object[] args)

    doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)

      initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd)

  最终在 initializeBean 内会调用如下方法,此处分别调用了对应的 setXXX 方法,外部信息得以注入

private void invokeAwareMethods(final String beanName, final Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}

  2. LoadTimeWeaverAware/ApplicationContextAware/ServletContextAware/MessageSourceAware 等

  这一类 Aware 是通过 BeanPostProcessor bean后置处理器实现外部信息注入的。

  同样在上述的 initializeBean 方法内会调用如下方法

public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException { Object result = existingBean;
for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
result = beanProcessor.postProcessBeforeInitialization(result, beanName);
if (result == null) {
return result;
}
}
return result;
}

此处会调用容器中注册的所有的 BeanPostProcessor 的 postProcessBeforeInitialization 方法,而 ApplicationContextAwareProcessor 是默认注册到容器内的,其 postProcessBeforeInitialization 方法内会调用如下方法:

private void invokeAwareInterfaces(Object bean) {
if (bean instanceof Aware) {
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 ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}
}

所以 bean 只要继承了 EnvironmentAware,ResourceLoaderAware 就具备相应的感知能力。

Spring的感知能力 Aware的更多相关文章

  1. 手写Spring,定义标记类型Aware接口,实现感知容器对象

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 同事写的代码,我竟丝毫看不懂! 大佬的代码,就像 "赖蛤蟆泡青蛙,张的丑玩 ...

  2. 想减少代码量,快设置一个有感知的 Aware Spring Bean

    摘要:正常情况下,Spring 中的 Bean 对 Spring 是无感知的,Spring 框架提供了这种扩展能力,能让一个 bean 成为有感知的. 本文分享自华为云社区<有感知的 Aware ...

  3. spring源码:Aware接口(li)

    一.spring容器中的aware接口介绍 Spring中提供了各种Aware接口,比较常见的如BeanFactoryAware,BeanNameAware,ApplicationContextAwa ...

  4. spring源码:Aware接口

    一.spring容器中的aware接口介绍 Spring中提供了各种Aware接口,比较常见的如BeanFactoryAware,BeanNameAware,ApplicationContextAwa ...

  5. Spring InitializingBean 接口以及Aware接口实现的原理

    关于Spring InitializingBean 接口以及Aware接口实现的其实都在 第11步中: finishBeanFactoryInitialization() 方法中完成了3部分的内容: ...

  6. Spring扩展点之Aware接口族

    引言 Spring中提供了各种Aware接口,方便从上下文中获取当前的运行环境,比较常见的几个子接口有:BeanFactoryAware,BeanNameAware,ApplicationContex ...

  7. Spring扩展之五:Aware接口等

    ApplicationContextAwareProcessor 1.介绍 ApplicationContextAwareProcessor是一个Spring内部工具,它实现了接口BeanPostPr ...

  8. spring扩展点之四:Spring Aware容器感知技术,BeanNameAware和BeanFactoryAware接口,springboot中的EnvironmentAware

    aware:英 [əˈweə(r)] 美 [əˈwer] adj.意识到的;知道的;觉察到的 XXXAware在spring里表示对XXX感知,实现XXXAware接口,并通过实现对应的set-XXX ...

  9. 对Spring aware理解

    aware翻译过来时就是意识到,我对他的理解就是spring的感知器.是不是很诡异这个名字起得^_^ 先来看看aware接口的结构 spring提供了许多的aware,Aware.java也只是做一个 ...

随机推荐

  1. control+shift + o热键冲突?????

    不知道有没有宝贝跟我遇到一样的问题 就是    control +shift+o    热键冲突了 进过我的严密调查. 这是因为你用的是A卡. 只要你把A卡换成N卡就可以了, 但是因为我太贫穷了,只能 ...

  2. 请求与上传文件,Session简介,Restful API,Nodemon

    作者 | Jeskson 来源 | 达达前端小酒馆 请求与上传文件 GET请求和POST请求 const express = require('express'); const app = expre ...

  3. [LeetCode] 896. Monotonic Array 单调数组

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  4. [LeetCode] 305. Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  5. [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  6. linux shell脚本中的延时

    linux shell脚本中的延时 还是使用 sleep 或usleep函数. 详细如下:     .sleep : 默认为秒. sleep 1s 表示延迟一秒   sleep 1m 表示延迟一分钟 ...

  7. okhttp 发送get post 请求

    package com.qlwb.business.util; import java.util.Map; import com.alibaba.fastjson.JSON; import okhtt ...

  8. oracle--数据库扩容后出现ORA-27102

    一,问题描述 Connected to an idle instance. SQL> startup nomount ORA: obsolete or deprecated parameter( ...

  9. lower_case_table_names与表格名称大小写的问题

    1 简介 在MySQL中,数据库对应数据目录中的目录.数据库中的每个表至少对应数据库目录中的一个文件(也可能是多个,取决于存储引擎).因此,所使用操作系统的大小写敏感性决定了数据库名和表名的大小写敏感 ...

  10. Kelp.Net是一个用c#编写的深度学习库

    Kelp.Net是一个用c#编写的深度学习库 基于C#的机器学习--c# .NET中直观的深度学习   在本章中,将会学到: l  如何使用Kelp.Net来执行自己的测试 l  如何编写测试 l  ...