Spring Aop源码分析

手动代理ProxyFactoryBean继承关系

# ProxyConfig 基础代理信息管理

# \—-AdvisedSupport 维护Advice和Advisor

#      \—-ProxyCreatorSupport 持有AopProxyFactory,能够创建AopProxy对象

#           \—-ProxyFactoryBean 实现了FactoryBean,具备生成bean代理的能力

ProxyFactoryBean实现了FactoryBean,调用过程如下

# ProxyFactoryBean.getObject

# \—-ProxyFactoryBean.initializeAdvisorChain 转换interceptorNames为若干PointcutAdvisor,保存到advisorArray,在JdkDynamicAopProxy.invoke中使用

# |    \—-BeanFactory.getBean 遍历interceptorNames,获取Advice实例

# |    \—-ProxyFactoryBean.addAdvisorOnChainCreation 

# |         \—-ProxyFactoryBean.namedBeanToAdvisor

# |         |    \—-DefaultAdvisorAdapterRegistry.wrap 把Advice封装成PointcutAdvisor

# |         \—-AdvisedSupport.addAdvisor    加到advisors末尾,更新advisorArray,此处增加的所有声明的Advisor,是否满足拦截条件在调用时检查!!!

# \—-ProxyFactoryBean.getSingletonInstance

#      \—-ProxyCreatorSupport.createAopProxy 此处将存储有代理配置信息的this传入,即ProxyFActoryBean对象

#      |    \—-DefaultAopProxyFactory.createAopProxy 利用工厂生成JdkDynamicAopProxy实例

#      \—-ProxyFactoryBean.getProxy  

#           \—-JdkDynamicAopProxy.getProxy 利用jdk动态代理技术生成代理对象

JdkDynamicAopProxy实现了InvocationHandler,调用过程如下

# JdkDynamicAopProxy.invoke

# \—-AdvisedSupport.getInterceptorsAndDynamicInterceptionAdvice 生成拦截器链

# |    \—-DefaultAdvisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice

# |         \—-AdvisedSupport.getAdvisors 返回advisorArray

# |         \—-DefaultAdvisorAdapterRegistry.getInterceptors  遍历Advisor,提取对应的多个MethodInterceptor

# |         |    \—-AdvisorAdapter.getInterceptor 适配成MethodInterceptor

# |         \—-PointcutAdvisor.getPointcut

# |         \—-Pointcut.getMethodMatcher

# |         \—-MethodMatchers.matches    如果匹配将提取到的拦截器加入到列表返回

# \—-new ReflectiveMethodInvocation() 生成拦截器及目标方法的调用链,责任链模式

# \—-ReflectiveMethodInvocation.proceed    

#      \—-MethodInterceptor.invoke 遍历拦截器,如果符合拦截条件调用其invoke方法

#      \—-ReflectiveMethodInvocation.invokeJoinpoint

#           \—-AopUtils.invokeJoinpointUsingReflection 调用被代理对象的真正目标方法

2.基于注解自动代理AnnotationAwareAspectJAutoProxyCreator继承关系

# ProxyConfig 基础代理信息管理

# \—-ProxyProcessorSupport 

#      \—-AbstractAutoProxyCreator 用于创建自动代理

#           \—-AbstractAdvisorAutoProxyCreator 用于创建自动代理时,查找配置文件中配置的Advisor

#                \—-AspectJAwareAdvisorAutoProxyCreator 

#                     \—-AnnotationAwareAspectJAutoProxyCreator 用于AspectJ注解的Advisor提取

AnnotationAwareAspectJAutoProxyCreator实现了BeanPostProcessor,调用过程如下

# AbstractAutoProxyCreator.postProcessAfterInitialization

# \—-AbstractAutoProxyCreator.wrapIfNecessary 检查是否需要返回当前bean的代理

#      \—-AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean 模板方法

#      |    \—-AbstractAdvisorAutoProxyCreator.findEligibleAdvisors 模板方法 

#      |         \—-AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors

#      |         |    \—-AbstractAdvisorAutoProxyCreator.findCandidateAdvisors

#      |         |    |    \—-BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans 寻找XML配置文件中配置的Advisor

#      |         |    \—-BeanFactoryAspectJAdvisorsBuilder.buildAspectJAdvisors 寻找AspectJ注解对应的Advisor

#      |         \—-AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply 删除不匹配的Advisor

#      |              \—-AopUtils.findAdvisorsThatCanApply

#      |                   \—-AopUtils.canApply 判断Pointcut和当前类是否匹配

#      |                        \—-MethodMatcher.matches

#      |                        \—-IntroductionAwareMethodMatcher.matches

#      \—-AbstractAutoProxyCreator.createProxy 利用前一步生成的intercetpors创建代理

#           \—-new ProxyFactory

#           \—-AbstractAutoProxyCreator.buildAdvisors 适配成Advisor

#           |    \—-DefaultAdvisorAdapterRegistry.wrap 

#           \—-ProxyFactory.getProxy

    原文作者:Spring Boot
    原文地址: https://blog.csdn.net/sinat_23002655/article/details/51476062
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞