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