Spring AOP源码分析
2021-07-20 14:04:35 6 举报
SpringAOP源码分析
作者其他创作
大纲/内容
代理对象
MethodBeforeAdvice
(2)AspectJ的5个通知中,AspectJMethodBeforeAdvice、AspectJAfterReturningAdvice没有实现MethodInterceptor接口,需要通过Adapter适配器将Advice转换为MethodInterceptor;
ThrowsAdviceInterceptor
若无异常,直接return;若有异常,catch(throwEx):advice.afterThrowing()throw throwEx;
AfterReturningAdvice
MethodInterceptor
MethodBeforeAdviceAdapter
ReflectiveMethodInvocation#proceed
Advice与Interceptor的适配转换
around()-->after部分return
invokeJoinpoint();执行目标方法
AspectJAroundAdvicearound();-->before部分-->pjp.proceed()-->pmi.proceed()(pjp就是MethodInvocationProceedingJoinPoint,对ProxyMethodInvocation做了代理)
AspectJAfterThrowingAdvice异常通知mi.proceed();
ThrowsAdviceAdapter
advice.after()return
CglibAopProxy.proceed(......);生成拦截器链chain,生成CglibMethodInvocation;CglibMethodInvocation.proceed()开始调用拦截器链;
AfterReturningAdviceAdapter
return
CglibAopProxy.processReturnTypereturn retVal返回给调用者。
(1)主要功能:将前置通知、返回通知等转化为方法拦截器;ThrowsAdvice是需要自定义实现的,AspectJ的异常通知AspectJAfterThrowingAdvice并未实现该接口;
AfterReturningAdviceInterceptor
advice.afterReturning()return
(3)适配器的原理:静态代理模式,很简单。
ThrowsAdvice
AfterReturningAdviceInterceptor(AspectJAfterReturningAdvice)mi.proceed();
AspectJMethodBeforeAdvice(MethodBeforeAdviceInterceptor)advice.before()
MethodBeforeAdviceInterceptor
AspectJAfterAdvicemi.proceed();
收藏
0 条评论
下一页