Spring Aop源码分析

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号:Java 技术驿站】 【加作者微信交流技术,拉技术群】
免费领取10G资料包与项目实战视频资料

手动代理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


来源:[]()

赞(0) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » Spring Aop源码分析

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏