Spring源码-xxxAware实现类和BeanPostProcessor接口调用过程

news2024/9/18 15:49:08

xxxAware实现类作用

以ApplicationContextAware接口为例
ApplicationContextAware的作用是可以方便获取Spring容器ApplicationContext,从而可以获取容器内的Bean


package org.springframework.context;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;

/**
public interface ApplicationContextAware extends Aware {

	/**
设置该对象运行的ApplicationContext。通常,这个调用将用于初始化对象。
<p>在填充普通bean属性之后调用,但在初始回调之前调用,
例如{@link org.springframework.beans.factory.InitializingBeanafterPropertiesSet()}
或自定义初始化方法。
在{@link ResourceLoaderAwaresetResourceLoader}, 
{@link ApplicationEventPublisherAwaresetApplicationEventPublisher}
和{@link MessageSourceAware}之后调用,如果适用的话。
@param applicationContext这个对象要使用的applicationContext对象
@在context初始化错误的情况下抛出
ApplicationContextException 
@如果被应用程序context方法抛出则抛出BeansException @参见org.springframework.beans.factory.BeanInitializationException
	 */
	void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

下面这个例子就是ApplicationContextUtil 实现了ApplicationContextAware 这个接口,重写了setApplicationContext(ApplicationContext applicationContext)方法,在调用到这个方法的时候给类里的applicationContext 属性赋值,后续别的方法在拿到这个applicationContext进行操作,比如根据getBean方法获取Bean对象以及调用beanFactory.registerBeanDefinition(beanName, definition);方法进行添加BeanDefinition即bean的元数据信息。

public class ApplicationContextUtil implements ApplicationContextAware {
 
  private static ApplicationContext applicationContext = null;
 
  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    if (this.applicationContext == null) {
      this.applicationContext = applicationContext;
    }
  }
  
  //获取applicationContext
  public static ApplicationContext getApplicationContext() {
    return applicationContext;
  }
 
  //通过name获取 Bean.
  public static Object getBean(String name) {
    return getApplicationContext().getBean(name);
  }
 
  //通过class获取Bean.
  public static <T> T getBean(Class<T> clazz) {
    return getApplicationContext().getBean(clazz);
  }
  
  /**
     * 同步方法注册bean到ApplicationContext中
     *
     * @param beanName
     * @param clazz
     * @param original bean的属性值
     */
  public static synchronized void setBean(String beanName, Class<?> clazz, Map<String,Object> original) {
    checkApplicationContext();
    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
    if(beanFactory.containsBean(beanName)){
      return;
    }
    //BeanDefinition beanDefinition = new RootBeanDefinition(clazz);
    GenericBeanDefinition definition = new GenericBeanDefinition();
    //类class
    definition.setBeanClass(clazz);
    //属性赋值
    definition.setPropertyValues(new MutablePropertyValues(original));
    //注册到spring上下文
    beanFactory.registerBeanDefinition(beanName, definition);
  }
  
}

上述是相关使用,为什么实现这个接口就会调用这个set方法,接下来根据spring源码,讲解xxxAware接口的前世今生。
不重要的步骤给出调用链路

AnnotationConfigApplicationContext## AnnotationConfigApplicationContext(String... basePackages) 构造函数
AbstractApplicationContext##refresh()容器刷新
AbstractApplicationContext##prepareBeanFactory(ConfigurableListableBeanFactory beanFactory)beanFactory的准备工作,对各种属性进行填充

prepareBeanFactory准备工作

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        // 添加beanPostProcessor,ApplicationContextAwareProcessor此类用来完成某些Aware对象的注入
		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
		// 设置要忽略自动装配的接口,很多同学理解不了为什么此处要对这些接口进行忽略,原因非常简单,这些接口的实现是由容器通过set方法进行注入的,
		// 所以在使用autowire进行注入的时候需要将这些接口进行忽略
		beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
		beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
		beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
		beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
	}

看下ApplicationContextAwareProcessor这个类:

/**
	 * 接口beanPostProcessor规定的方法,会在bean创建时,实例化后,初始化前,对bean对象应用
	 * @param bean the new bean instance
	 * @param beanName the name of the bean
	 * @return
	 * @throws BeansException
	 */
	@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)){
			return bean;
		}

		AccessControlContext acc = null;

		if (System.getSecurityManager() != null) {
			acc = this.applicationContext.getBeanFactory().getAccessControlContext();
		}

		if (acc != null) {
			AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
				// 检测bean上是否实现了某个aware接口,有的话进行相关的调用
				invokeAwareInterfaces(bean);
				return null;
			}, acc);
		}
		else {
			invokeAwareInterfaces(bean);
		}

		return bean;
	}
	
	

在这个类postProcessBeforeInitialization(Object bean, String beanName)这个方法内部会调用invokeAwareInterfaces(Object bean) 这个方法,判断bean的类型是属于哪一个xxxAware接口然后执行对应的setXXX()方法,至此和前边实现ApplicationContextAware接口重写setApplicationContext()方法串起来了,在哪里调用setApplicationContext方法完成。

/**
	 * 如果某个bean实现了某个aware接口,给指定的bean设置相应的属性值
	 *
	 * @param 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 ApplicationContextAware) {
			((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
		}
	}

还有一个问题,ApplicationContextAwareProcessor这个类的postProcessBeforeInitialization(Object bean, String beanName) 方法什么时候在哪里进行调用呢?这个类实现了BeanPostProcessor这个接口

/**
 * bean的后置处理器接口,在依赖注入的初始化方法前后进行调用
 */
public interface BeanPostProcessor {

	/**
	 * 初始化方法调用前要进行的处理逻辑
	 */
	@Nullable
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	/**
	 * 在初始化方法指定后要进行的处理逻辑
	 */
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

}

接下来看实现了BeanPostProcessor 这个接口的实现类在哪里会被触发调用里面的postProcessBeforeInitialization方法!!!!
通过看调用链路,下面是整个过程是在完成实例化,属性填充,执行初始化的时候在initializeBean方法调用 applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
在这里插入图片描述
applyBeanPostProcessorsBeforeInitialization细节,对实现BeanPostProcessor 的类进行调用postProcessBeforeInitialization这个方法,这里会调用到ApplicationContextAwareProcessor的postProcessBeforeInitialization方法来对invokeAwareInterfaces实现Aware接口的类进行set操作

	public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
			throws BeansException {

		//初始化返回结果为existingBean
		Object result = existingBean;
		//遍历 该工厂创建的bean的BeanPostProcessors列表
		for (BeanPostProcessor processor : getBeanPostProcessors()) {
			// postProcessBeforeInitialization:在任何Bean初始化回调之前(如初始化Bean的afterPropertiesSet或自定义的init方法)
			// 将此BeanPostProcessor 应用到给定的新Bean实例。Bean已经填充了属性值。返回的Bean实例可能时原始Bean的包装器。
			// 默认实现按原样返回给定的 Bean
			Object current = processor.postProcessBeforeInitialization(result, beanName);
			// 如果 current为null
			if (current == null) {
				//直接返回result,中断其后续的BeanPostProcessor处理
				return result;
			}
			//让result引用processor的返回结果,使其经过所有BeanPostProcess对象的后置处理的层层包装
			result = current;
		}
		//返回经过所有BeanPostProcess对象的后置处理的层层包装后的result
		return result;
	}

同时,在完成实例化,属性填充,执行初始化的时候在initializeBean方法调用 applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);将BeanPostProcessors应用到给定的现有Bean实例,调用它们的postProcessAfterInitialization方法。

@Override
	public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
			throws BeansException {

		//初始化结果对象为result,默认引用existingBean
		Object result = existingBean;
		//遍历该工厂创建的bean的BeanPostProcessors列表
		for (BeanPostProcessor processor : getBeanPostProcessors()) {
			//回调BeanPostProcessor#postProcessAfterInitialization来对现有的bean实例进行包装
			Object current = processor.postProcessAfterInitialization(result, beanName);
			//一般processor对不感兴趣的bean会回调直接返回result,使其能继续回调后续的BeanPostProcessor;
			// 但有些processor会返回null来中断其后续的BeanPostProcessor
			// 如果current为null
			if (current == null) {
				//直接返回result,中断其后续的BeanPostProcessor处理
				return result;
			}
			//让result引用processor的返回结果,使其经过所有BeanPostProcess对象的后置处理的层层包装
			result = current;
		}
		//返回经过所有BeanPostProcess对象的后置处理的层层包装后的result
		return result;
	}

至此,BeanPostProcessor接口的调用以及实现xxxAware的类的调用使用处理过程讲解结束

    初始化方法调用前要进行的处理逻辑
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}
	在初始化方法指定后要进行的处理逻辑
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1840846.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

百度一下首页制作(HTML+CSS)

部分代码展示&#xff1a; <!DOCTYPE html> <html> <head><meta charset"utf-8"><title>百度一下&#xff0c;你就知道</title><style type"text/css">/*清除元素默认性质*/body { margin: 0;padding: 0;list-…

白酒:酒文化的教育价值与实践

酒文化作为中国传统文化的重要组成部分&#xff0c;具有丰富的教育价值。云仓酒庄的豪迈白酒作为酒文化的品牌之一&#xff0c;在传承与发展中不断挖掘和发挥酒文化的教育价值。 首先&#xff0c;豪迈白酒有责任传承丰富的历史文化知识。从酒的起源、酿造技艺、酒器文化到酒礼酒…

NoMaD: Goal Masked Diffusion Policies for Navigation and Exploration

引言 机器人学习的背景和挑战 本文的研究重点 现有方法的局限性 本文的创新点 相关工作 事先准备 视觉目标条件策略 ViNT在目标条件导航中表现出最先进的性能&#xff0c;但它不能执行无方向探索&#xff0c;需要外部的子目标建议机制。Nomad扩展了Vint&#xff0c;同时支持目…

【PyQt5】一文向您详细介绍 QHBoxLayout() 的作用

【PyQt5】一文向您详细介绍 QHBoxLayout() 的作用 下滑即可查看博客内容 &#x1f308; 欢迎莅临我的个人主页 &#x1f448;这里是我静心耕耘深度学习领域、真诚分享知识与智慧的小天地&#xff01;&#x1f387; &#x1f393; 博主简介&#xff1a;985高校的普通本硕&a…

忘记 iPhone 密码:如果忘记密码,如何解锁 iPhone

为了提高个人数据的安全性&#xff0c;用户通常会为不同的帐户和设备创建不同的复杂密码。虽然较新的 iPhone 型号具有生物识别和面部解锁功能&#xff0c;但这些功能并不总是有效 - 如果您忘记了 iPhone 的密码&#xff0c;您可能会遇到麻烦。 iPhone 用户和 Android 用户一样…

docker ce的使用介绍

docker docker17.03以后 docker ce&#xff0c;社区免费版&#xff0c;vscode的docker插件使用的该版本&#xff08;默认windows只支持windows容器&#xff0c;linux支持linux容器&#xff09;docker ee&#xff0c;企业版本 docker17.03以前 docker toolbox&#xff0c;基于…

Python4 操作MySQL数据库

通过python的pymysql库连接到本地的MySQL数据库&#xff0c;并执行查询操作来获取数据&#xff0c;然后打印出每一行的数据&#xff0c;这里以一个简单的学生表为例进行介绍。 1. MySQL的安装与数据准备 首先需要安装MySQL&#xff0c;在安装完成之后使用Navicat与本地数据库…

IFM易福门SV7500SV4200涡街流量计型号都是进口的。

IFM易福门SV7500SV4200涡街流量计型号都是进口的。工程余料。

华为---OSPF单区域配置(一)

09、OSPF 9.1 OSPF单区域配置 9.1.1 原理概述 为了弥补距离矢量路由协议的不足&#xff0c;IETF组织开发了一种基于链路状态的内部网关协议——OSPF&#xff08;Open Shortest Path First&#xff0c;开放式最短路径优先&#xff09;。 OSPF作为基于链路状态的协议&#xf…

Python开发记录--手撸加解密小工具(1)

目录 1.环境搭建 2. PyQt简介 3.小结 最近考虑设计一个涵盖国际、国家等加密算法的小工具&#xff0c;用于组内测试使用。一来回顾下算法&#xff0c;二来很久没写代码得练练手感&#xff0c;给工具取个名字&#xff1a;MuscleV0.1 MuscleV0.1涵盖的算法&#xff1a; 对称算…

ATA-2032高压放大器设计要求是什么内容

高压放大器是一种专门用于放大高电压信号的电子设备。它在许多领域都具有重要的应用&#xff0c;例如在医疗设备、科学研究、传感器驱动和测试测量等方面。设计一个高压放大器需要考虑多个因素&#xff0c;包括性能要求、安全性、稳定性和可靠性等方面。下面我们将详细讨论高压…

【计算机网络】[第4章 网络层][自用]

1 概述 (1)因特网使用的TCP/IP协议体系(四层)的网际层,提供的是无连接、不可靠的数据报服务; (2)ATM、帧中继、X.25的OSI体系(七层)中的网络层,提供的是面向连接的、可靠的虚电路服务。 (3)路由选择分两种: 一种是由用户or管理员人工进行配置(只适用于规…

第58章SOCKET:TCP/IP网络基础

58.1 互联网 互联网会将不同的计算机网络连接起来并允许位于网络中的主机相互之间进行通信。互联网的目标是隐藏不同物理网络的细节以便向互联网中的所有主机呈现一个统一的网络架构&#xff0c;TCP/IP已经成了使用最为广泛的协议套件了&#xff0c; 术语Internet被用来指将全球…

c++文件io,字符串io简单介绍

目录 c文件io 介绍 采用文件流对象操作文件的一般步骤 示例 注意点 利用字节流特性 字符串io 介绍 istringstream ostringstream 示例 c文件io 介绍 c根据文件内容的数据格式分为二进制文件和文本文件 基本上和c一样 c 标准库中有许多不同的标志 用于指定流对象的…

【Java】已解决java.util.EmptyStackException异常

文章目录 一、问题背景二、可能出错的原因三、错误代码示例四、正确代码示例五、注意事项 已解决java.util.EmptyStackException异常 一、问题背景 java.util.EmptyStackException是Java在使用java.util.Stack类时可能会遇到的一个异常。这个异常通常在尝试从空的栈中弹出&am…

CVE-2020-1957 漏洞复现

先声明一下&#xff0c;免杀还是会更的&#xff0c;不过中间可能会穿插一下渗透的内容&#xff01;&#xff01;&#xff01; 踩坑点&#xff1a; 在一开始翻阅了CSDN之后&#xff0c;发现不同文章之间存在出入&#xff0c;于是最后去了CVE的官方文档&#xff0c;和参考一些国…

JAVA每日作业day6.19

ok了家人们今天继续学习面向对象&#xff0c;话不多说看看今天学了什么 一.面向对象-封装 1&#xff0c;private private:私有的 权限修饰符 是一个成员修饰符&#xff0c;修饰成员变量 被private修饰的成员变量和成员方法只能在本类中使用 对外访问使用是 set XXX 和 get X…

Redis缓存与数据库双写不一致及解决方法

1.缓存与数据库双写不一致 在大并发下&#xff0c;同时操作数据库与缓存会存在数据不一致性问题 1.1 双写不一致情况 1.2 读写并发不一致 2.解决方法 对于并发几率很小的数据(如个人维度的订单数据、用户数据等)&#xff0c;这种几乎不用考虑这个问题&#xff0c;很少会发生…

小主机折腾记录27

1.买了一个9600k&#xff0c;3根台电 4G 2666 极光A40&#xff0c;一根台电8G2666 极光A40&#xff0c;一根国惠8G2666&#xff0c;一个惠普3热管散热器 测试结果如下 1&#xff09;三根台电 4G2666 相互兼容&#xff0c;频率2667显示正常&#xff0c;显示为美光颗粒&#xff0…

React state(及组件) 的保留与重置

当在树中相同的位置渲染相同的组件时&#xff0c;React 会一直保留着组件的 state return (<div><Counter />{showB && <Counter />} </div> ) // 当 showB 为 false, 第二个计数器停止渲染&#xff0c;它的 state 完全消失了。这是因为 React…