欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring aop方法拦截器链

程序员文章站 2022-06-02 23:22:57
final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable {} ]public Object invoke(Object proxy, Method method, Object[] arg... ......
final class jdkdynamicaopproxy implements aopproxy, invocationhandler, serializable {}
]public object invoke(object proxy, method method, object[] args)
 >object retval;
 >list<object> chain = this.advised.getinterceptorsanddynamicinterceptionadvice(method, targetclass);
 >if (chain.isempty()) {
 >+object[] argstouse = aopproxyutils.adaptargumentsifnecessary(method, args);
 >+retval = aoputils.invokejoinpointusingreflection(target, method, argstouse);
 >}
 >else {
 >+invocation = new reflectivemethodinvocation(proxy, target, method, args, targetclass, chain);//链式执行拦截(通知)方法
 >+retval = invocation.proceed();
 >}

[reflectivemethodinvocation.proceed()]
public object proceed() throws throwable {
        //    we start with an index of -1 and increment early.
        if (this.currentinterceptorindex == this.interceptorsanddynamicmethodmatchers.size() - 1) {
            return invokejoinpoint();
        }

        object interceptororinterceptionadvice =
                this.interceptorsanddynamicmethodmatchers.get(++this.currentinterceptorindex);
        if (interceptororinterceptionadvice instanceof interceptoranddynamicmethodmatcher) {
            // evaluate dynamic method matcher here: static part will already have
            // been evaluated and found to match.
            interceptoranddynamicmethodmatcher dm =
                    (interceptoranddynamicmethodmatcher) interceptororinterceptionadvice;
            if (dm.methodmatcher.matches(this.method, this.targetclass, this.arguments)) {
//调用拦截器的执行方法,拦截器执行拦截逻辑后继续调用目标方法的proceed()方法,参考下面的两个拦截器invoke()实现 return dm.interceptor.invoke(this); } else { // dynamic matching failed. // skip this interceptor and invoke the next in the chain. return proceed(); } } else { // it's an interceptor, so we just invoke it: the pointcut will have // been evaluated statically before this object was constructed. return ((methodinterceptor) interceptororinterceptionadvice).invoke(this); } } //before advice方法拦截器 public class methodbeforeadviceinterceptor implements methodinterceptor, beforeadvice, serializable { private final methodbeforeadvice advice; /** * create a new methodbeforeadviceinterceptor for the given advice. * @param advice the methodbeforeadvice to wrap */ public methodbeforeadviceinterceptor(methodbeforeadvice advice) { assert.notnull(advice, "advice must not be null"); this.advice = advice; } @override public object invoke(methodinvocation mi) throws throwable { this.advice.before(mi.getmethod(), mi.getarguments(), mi.getthis()); return mi.proceed(); } } //after advice方法拦截器 public class aspectjafteradvice extends abstractaspectjadvice implements methodinterceptor, afteradvice, serializable { public aspectjafteradvice( method aspectjbeforeadvicemethod, aspectjexpressionpointcut pointcut, aspectinstancefactory aif) { super(aspectjbeforeadvicemethod, pointcut, aif); } @override public object invoke(methodinvocation mi) throws throwable { try { return mi.proceed(); } finally { invokeadvicemethod(getjoinpointmatch(), null, null); } } @override public boolean isbeforeadvice() { return false; } @override public boolean isafteradvice() { return true; } }