
Spring-12 深入理解 AOP 底层 API
Spring 源码系列文章会遵循由浅入深,由易到难,由宏观到微观的原则,目标是尽量降低学习难度,而不是一上来就迷失在源码当中. 文章会从一个场景作为出发点,针对性的目的性极强的针对该场景对 Spring 的实现原理,源码进行探究学习。该系列文章会让你收获什么? 从对 Spring 的使用者成为 Spring 专家。该文章会同步在微信公众号 【DevXJava】, 方便在微信客户端阅读。
本章会直接使用
springAOP的底层 API ,帮助读者更加直观深入的理解AOP的执行原理。废话少说上代码。
org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [AspectJExpressionPointcut: () execution(* foo())]; advice [org.springframework.aop.aspectj.AspectJAfterAdvice: advice method [public void org.devx.spring.certified.professional.edu.course.a18.A18$Aspect.after()]; aspect name '']
org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [AspectJExpressionPointcut: () execution(* foo())]; advice [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice: advice method [public void org.devx.spring.certified.professional.edu.course.a18.A18$Aspect.before1()]; aspect name '']
org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [AspectJExpressionPointcut: () execution(* foo())]; advice [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice: advice method [public void org.devx.spring.certified.professional.edu.course.a18.A18$Aspect.before2()]; aspect name '']
org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [AspectJExpressionPointcut: () execution(* foo())]; advice [org.springframework.aop.aspectj.AspectJAroundAdvice: advice method [public java.lang.Object org.devx.spring.certified.professional.edu.course.a18.A18$Aspect.around(org.aspectj.lang.ProceedingJoinPoint) throws java.lang.Throwable]; aspect name '']
org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [AspectJExpressionPointcut: () execution(* foo())]; advice [org.springframework.aop.aspectj.AspectJAfterReturningAdvice: advice method [public void org.devx.spring.certified.professional.edu.course.a18.A18$Aspect.afterReturning()]; aspect name '']
org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [AspectJExpressionPointcut: () execution(* foo())]; advice [org.springframework.aop.aspectj.AspectJAfterThrowingAdvice: advice method [public void org.devx.spring.certified.professional.edu.course.a18.A18$Aspect.afterThrowing()]; aspect name '']
org.springframework.aop.interceptor.ExposeInvocationInterceptor.ADVISOR
>>>>>>>>>>>>>>>>>>>>>>>>> org.springframework.aop.interceptor.ExposeInvocationInterceptor@56a6d5a6
>>>>>>>>>>>>>>>>>>>>>>>>> org.springframework.aop.aspectj.AspectJAfterAdvice: advice method [public void org.devx.spring.certified.professional.edu.course.a18.A18$Aspect.after()]; aspect name ''
>>>>>>>>>>>>>>>>>>>>>>>>> org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor@18ce0030
>>>>>>>>>>>>>>>>>>>>>>>>> org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor@4445629
>>>>>>>>>>>>>>>>>>>>>>>>> org.springframework.aop.aspectj.AspectJAroundAdvice: advice method [public java.lang.Object org.devx.spring.certified.professional.edu.course.a18.A18$Aspect.around(org.aspectj.lang.ProceedingJoinPoint) throws java.lang.Throwable]; aspect name ''
>>>>>>>>>>>>>>>>>>>>>>>>> org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor@45b9a632
>>>>>>>>>>>>>>>>>>>>>>>>> org.springframework.aop.aspectj.AspectJAfterThrowingAdvice: advice method [public void org.devx.spring.certified.professional.edu.course.a18.A18$Aspect.afterThrowing()]; aspect name ''
@Aspect @Before1 ------------------------------
@Aspect @Before2 ------------------------------
@Aspect @Around before -------------------------------
Target foo
@Aspect @AfterReturning -------------------------------
@Aspect @Around after -------------------------------
@Aspect @After -------------------------------
我们完全使用 spring 的底层 API 完成了 AOP 功能。通过 ReflectiveMethodInvocation 进行了方法的调用,这里如果使用 proxy.foo() 其结果是一样的。
Advic 转换为 MethodInterceptor
在代理对象调用方法执行过程中所有的
Advic都要被转换为MethodInterceptor这样的环绕形式才能工作。
@Before 注解对应的 MethodBeforeAdvice 会被转换为 MethodBeforeAdviceInterceptor。

@After 注解对应的 AspectJAfterAdvice 自身实现了 MethodInterceptor 接口所以不需要经过转换。

@Around 注解对应的 AspectJAroundAdvice 自身实现了 MethodInterceptor 接口所以不需要经过转换。

@AfterReturning 注解对应的 AfterReturningAdvice 会被转换为 AfterReturningAdviceInterceptor。
@AfterThrowing 注解对应的 AspectJAfterThrowingAdvice 自身实现了 MethodInterceptor 接口所以不需要经过转换。

转换工作是由 AdvisorAdapter 完成,例如 MethodBeforeAdviceAdapter 将 MethodBeforeAdvice 转换为 MethodBeforeAdviceInterceptor。


ReflectiveMethodInvocation 调用链
巧妙的使用了
责任链设计模式,理解了这个就很容易理解代理对象的执行过程。如果读者仔细阅读了前面内容就会发现所有的MethodInterceptor实现中在invoke方法中都去调用了MethodInvocation的proceed方法。

代理对象内部封装的简要示意图,也可以把它理解为看源码的一个顺序或入口.

DevX 会持续分享 Java 技术干货,如果你觉得本文对你有帮助希望你可以分享给更多的朋友看到。该文章会同步在微信公众号 【DevXJava】, 方便在微信客户端阅读。
DevX 不止于技术

本篇文章来源于微信公众号: DevXJava
微信扫描下方的二维码阅读本文

Comments NOTHING