三、计算拦截器链
调用链图:
JdkDynamicAopProxy实现了InvocationHandler接口,当其创建的JDK Proxy上的某一个方法被调用时,将会被JdkDynamicAopProxy类中的invoke(..)方法拦截:
final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { ... try { ... target = targetSource.getTarget(); if (target != null) { targetClass = target.getClass(); } // 计算拦截器链 List chain = this.advised.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice( this.advised, proxy, method, targetClass); if (chain.isEmpty()) { // 拦截器链为空,直接调用目标对象上的方法 retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args); } else { // 调用方法 invocation = new ReflectiveMethodInvocation( proxy, target, method, args, targetClass, chain); retVal = invocation.proceed(); } ... return retVal; } finally { ... } } ... }
AdvisedSupport中AdvisorChainFactory的默认实现是HashMapCachingAdvisorChainFactory,它能将某一方法上的拦截器链进行缓存。
public final class HashMapCachingAdvisorChainFactory implements AdvisorChainFactory { public List getInterceptorsAndDynamicInterceptionAdvice( Advised config, Object proxy, Method method, Class targetClass) { private final Map methodCache = CollectionFactory.createIdentityMapIfPossible(32); List cached = (List) this.methodCache.get(method); if (cached == null) { // recalculate cached = AdvisorChainFactoryUtils.calculateInterceptorsAndDynamicInterceptionAdvice( config, proxy, method, targetClass); this.methodCache.put(method, cached); } return cached; } ... }
工具类AdvisorChainFactoryUtils提供了计算拦截器的方法:
public abstract class AdvisorChainFactoryUtils { public static List calculateInterceptorsAndDynamicInterceptionAdvice( Advised config, Object proxy, Method method, Class targetClass) { List interceptorList = new ArrayList(config.getAdvisors().length); ... AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance(); for (int i = 0; i < config.getAdvisors().length; i++) { //遍历每一个Advisor Advisor advisor = config.getAdvisors()[i]; if (advisor instanceof PointcutAdvisor) { // add it conditionally PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor; // 计算该Advisor是否适合该class if (pointcutAdvisor.getPointcut().getClassFilter().matches(targetClass)) { // 将advisor持有的before、after、throws等advice转化为统一 // 的MethodInterceptor Interceptor[] interceptors = registry.getInterceptors(advisor); MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher(); // 计算该Advisor是否适合该method if (methodMatches(mm,method, targetClass,hasIntroductions)) { if (mm.isRuntime()) { ... //动态interceptor } else { // 将该Advisor加入method的interceptor列表 interceptorList.addAll(Arrays.asList(interceptors)); } } } } else if (advisor instanceof IntroductionAdvisor) { ... } } return interceptorList; } ... }
相关推荐
AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析...
springAOP核心源码流程分析,请基于5.0.6版本进行代码跟进。
spring aop框架源码级分析 结构图
有关于Spring,我们最常用的两个功能就是IOC和AOP,前几篇文章从源码级别介绍了Spring容器如何为我们生成bean及bean之间的依赖关系 下面我们接着来看AOP的源码实现。 有关于AOP,我们在面试中也被无数次问到...
springAOP核心源码流程分析,请基于5.0.6版本进行代码跟进。
NULL 博文链接:https://ylxy3058.iteye.com/blog/2224244
Spring源代码解析(二):ioc容器在Web容器中的启动 Spring源代码分析(三):Spring JDBC Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy Spring源代码解析(六):Spring声明式事务处理...
Spring AOP(面向切面编程)作为Spring框架的一个重要部分,为Java开发者提供了一个...通过对Spring AOP源码的深入分析,我们可以更好地理解其内部工作原理,从而在实际开发中更加灵活和高效地使用这一强大的编程范式。
元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运维提供支持 日志记录尽可能减少性能影响
本文深入分析了Spring AOP的实现机制,让读者能够更好地理解和应用这一强大的编程范式。 Spring AOP是基于代理模式实现的,主要包括动态代理、通知(Advice)、切点(Pointcut)、切面(Aspect)和连接点(Join ...
面试必考之HashMap源码分析与实现 探索JVM底层奥秘ClassLoader源码分析与案例讲解 面试必备技能之Dubbo企业实战 ...互联网系统垂直架构之Session解决方案 分库分表之后分布式下如何...无处不在的Spring AOP事务及踩过的坑
SpringAOP入门和原理分析,动态代理
aop分析笔记 个人总结所得 org.springframework.aop.framework.autoproxy
Java流行框架源码分析:Spring源码、SpringBoot源码、SpringAOP源码、SpringSecurity源码、SpringSecurity OAuth2源码、JDK源码、Netty源码
spring version: 5.0.0; jdk: 1.8 IOC大致调用顺序(IOC调用的AOP标签解析)
老王读 Spring AOP 系列文章的源码分析&测试代码 帮助大家学习 Spring 源码,更加了解 Spring 的底层 博客专栏地址:https://blog.csdn.net/wang489687009/category_11269905.html
主要介绍了Spring AOP中使用args表达式的方法,结合实例形式分析了spring面向切面AOP中使用args表达式具体步骤、相关实现技巧与操作注意事项,需要的朋友可以参考下
Spring_AOP入门与分析,深入分析技术精髓。
主要介绍了spring AOP定义AfterThrowing增加处理,结合实例形式分析了spring面向切面AOP定义AfterThrowing相关实现步骤与操作技巧,需要的朋友可以参考下
那么面向切面编程AOP(Aspect-OrientedProgramming)则可以对系统需求进行很好的模软件开发经历了从汇编语言到高级语言和从过程化编程到面向对象编程;前者是为了提高开发效率,而后者则使用了归纳法,把具有共性的...