首页 > 编程语言 >SpringAOP源码分析

SpringAOP源码分析

时间:2022-09-26 04:04:30浏览次数:64  
标签:分析 support factory springframework 源码 beans aop SpringAOP org

AOP源码流程

源码基于:Spring5.3.23版本

当Spring开启切面后,会向Spring容器中添加一个BeanPostProcessor,即AbstractAutoProxyCreator,即对象通过反射初始化后执行的一个处理器。

首先,AOP开启注解是@EnableAspectJAutoProxy,点进去,有个@Import(AspectJAutoProxyRegistrar.class),该类会被Spring加载,并且该类继承自ImportBeanDefinitionRegistrar类,主要是向Spring中添加BeanDefinition,SpringIoc容器初始化时先从xml或者注解中找到BeanDefinition,然后再从ImportBeanDefinitionRegistrar中找BeanDefinition。收集完所有BeanDefinition后,反射对这些BeanDefinition进行初始化对象。这也是很多自动配置的实现原理。

@EnableAspectJAutoProxy注解添加BeanPostProcessor的堆栈如下:

registerOrEscalateApcAsRequired:140, AopConfigUtils (org.springframework.aop.config)
registerAspectJAnnotationAutoProxyCreatorIfNecessary:100, AopConfigUtils (org.springframework.aop.config)
registerAspectJAnnotationAutoProxyCreatorIfNecessary:93, AopConfigUtils (org.springframework.aop.config)
registerBeanDefinitions:45, AspectJAutoProxyRegistrar (org.springframework.context.annotation)
registerBeanDefinitions:86, ImportBeanDefinitionRegistrar (org.springframework.context.annotation)
lambda$loadBeanDefinitionsFromRegistrars$1:396, ConfigurationClassBeanDefinitionReader (org.springframework.context.annotation)
accept:-1, 824208363 (org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader$$Lambda$51)
forEach:684, LinkedHashMap (java.util)
loadBeanDefinitionsFromRegistrars:395, ConfigurationClassBeanDefinitionReader (org.springframework.context.annotation)
loadBeanDefinitionsForConfigurationClass:157, ConfigurationClassBeanDefinitionReader (org.springframework.context.annotation)
loadBeanDefinitions:129, ConfigurationClassBeanDefinitionReader (org.springframework.context.annotation)
processConfigBeanDefinitions:343, ConfigurationClassPostProcessor (org.springframework.context.annotation)
postProcessBeanDefinitionRegistry:247, ConfigurationClassPostProcessor (org.springframework.context.annotation)
invokeBeanDefinitionRegistryPostProcessors:311, PostProcessorRegistrationDelegate (org.springframework.context.support)
invokeBeanFactoryPostProcessors:112, PostProcessorRegistrationDelegate (org.springframework.context.support)
invokeBeanFactoryPostProcessors:746, AbstractApplicationContext (org.springframework.context.support)
refresh:564, AbstractApplicationContext (org.springframework.context.support)
main:20, SpringTestApplication (com.zanpo.it.springtest)

断点断下可以看出,实际类型是:org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator

BeanPostProcessoor会在反射创建所有Bean中的对象,并完成依赖注入后执行。参考IOC初始化流程。

Aop的BeanPostProcessoor的执行堆栈如下:

postProcessAfterInitialization:290, AbstractAutoProxyCreator (org.springframework.aop.framework.autoproxy)
applyBeanPostProcessorsAfterInitialization:455, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
initializeBean:1808, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
doCreateBean:620, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:542, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
lambda$doGetBean$0:335, AbstractBeanFactory (org.springframework.beans.factory.support)
getObject:-1, 712025048 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$43)
getSingleton:234, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:333, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:208, AbstractBeanFactory (org.springframework.beans.factory.support)
preInstantiateSingletons:955, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:918, AbstractApplicationContext (org.springframework.context.support)
refresh:583, AbstractApplicationContext (org.springframework.context.support)
main:20, SpringTestApplication (com.zanpo.it.springtest)

关键的就是这个wrapIfNecessary方法了。

接下来该处理器会将原对象和切面方法包装成一个JdkDynamicAopProxy对象,并对原对象的接口和一些Spring对象接口创建动态对立对象,动态代理对象handler为这个JdkDynamicAopProxy对象。

执行堆栈如下(jdk动态代理):

getProxy:126, JdkDynamicAopProxy (org.springframework.aop.framework)
getProxy:110, ProxyFactory (org.springframework.aop.framework)
createProxy:480, AbstractAutoProxyCreator (org.springframework.aop.framework.autoproxy)
wrapIfNecessary:344, AbstractAutoProxyCreator (org.springframework.aop.framework.autoproxy)
postProcessAfterInitialization:293, AbstractAutoProxyCreator (org.springframework.aop.framework.autoproxy)
applyBeanPostProcessorsAfterInitialization:455, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
initializeBean:1808, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
doCreateBean:620, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:542, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
lambda$doGetBean$0:335, AbstractBeanFactory (org.springframework.beans.factory.support)
getObject:-1, 712025048 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$43)
getSingleton:234, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:333, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:208, AbstractBeanFactory (org.springframework.beans.factory.support)
preInstantiateSingletons:955, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:918, AbstractApplicationContext (org.springframework.context.support)
refresh:583, AbstractApplicationContext (org.springframework.context.support)
main:20, SpringTestApplication (com.zanpo.it.springtest)

注意这里的代理对象是针对原对象接口和几个Spring接口(SpringProxy、Advised、DecoratingProxy)创建的。所以,我们可以对原对象使用instanceof SpringProxy判断是否是代理对象,或者将代理对象强转为Advised接口或者DecoratingProxy接口,获取一些关于代理对象的信息。

在执行原对象的方法时,会进入该JdkDynamicAopProxy类对象的invoke方法,执行切面方法然后反射执行JdkDynamicAopProxy中的代理对象方法。

执行原方法堆栈如下:

invokeJoinpointUsingReflection:344, AopUtils (org.springframework.aop.support)
invokeJoinpoint:198, ReflectiveMethodInvocation (org.springframework.aop.framework)
proceed:163, ReflectiveMethodInvocation (org.springframework.aop.framework)
invoke:49, AspectJAfterAdvice (org.springframework.aop.aspectj)
proceed:186, ReflectiveMethodInvocation (org.springframework.aop.framework)
invoke:58, MethodBeforeAdviceInterceptor (org.springframework.aop.framework.adapter)
proceed:186, ReflectiveMethodInvocation (org.springframework.aop.framework)
invoke:97, ExposeInvocationInterceptor (org.springframework.aop.interceptor)
proceed:186, ReflectiveMethodInvocation (org.springframework.aop.framework)
invoke:215, JdkDynamicAopProxy (org.springframework.aop.framework)
sayHi:-1, $Proxy22 (com.sun.proxy)
main:23, SpringTestApplication (com.zanpo.it.springtest)

可以看出最终还是通过反射进行的调用

标签:分析,support,factory,springframework,源码,beans,aop,SpringAOP,org
From: https://www.cnblogs.com/zanpocc/p/16729559.html

相关文章

  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制......
  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制......
  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践(一)基本要求拓扑文件#!/usr/bin/envpythonfrommininet.netimportMininetfrommininet.nodeimportController,RemoteController,O......
  • Wireshark网络分析笔记
    Wireshark简介Wireshark是使用最广泛的一款「开源抓包软件」,常用来检测网络问题、攻击溯源、或者分析底层通信机制。它使用WinPCAP作为接口,直接与网卡进行数据报文交换。......
  • 实验3:OpenFlow协议分析实践
    实验报告(一)基本要求1.导入到/home/用户名/学号/lab3/目录下的拓扑文件2.Wireshark抓包结果OFPT_HELLO控制器6633端口(我最高能支持OpenFlow1.0)--->交换机3892......
  • 实验3:OpenFlow协议分析实践
    一、基本要求1.搭建下图所示拓扑,完成相关IP配置,并实现主机与主机之间的IP通信。用抓包软件获取控制器与交换机之间的通信数据。控制器6633端口(我最高能支持OpenFlo......
  • 三--词法分析及语法分析程序--1.设计扫描器时应考虑的几个问题
     1.词法分析器的功能与实现方式  2.源程序的输入及预处理3.单词符号的内部表示--词法分析器的输出形式......
  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践一、实验目的1.能够运用wireshark对OpenFlow协议数据交互过程进行抓包;2.能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与......
  • [数值分析]埃特肯加速法求方程解Aitken!
    埃特肯加速法求方程解Aitken!importnumpyasnpimportmatplotlib.pyplotaspltdeff(x):returnx**3-x**2-1构造迭代函数\[\begin{aligned}x^{3}-x......
  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制......