• spring aop实现过程之三Spring AOP中Aspect编织的实现


    1.上节我们谈到拦截器起作用时,实现代码(ReflectiveMethodInvocation.java)如下:

        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)) {
                    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);
            }
        }

    2.前置Advice MethodBeforeAdviceInterceptor.java

        public Object invoke(MethodInvocation mi) throws Throwable {
            this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis() );
            return mi.proceed();
        }

    3.后置Advice AfterReturningAdviceInterceptor.java

        public Object invoke(MethodInvocation mi) throws Throwable {
            Object retVal = mi.proceed();
            this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis());
            return retVal;
        }
  • 相关阅读:
    【codevs1227】方格取数2(最大流费最大流-模板
    【ZJOI2008】【BZOJ1033】杀蚂蚁(占坑待填
    python基础学习1-流程控制和判断
    python基础学习1-变量定义赋值,屏幕输入输出
    Jzoj5237 最长公共子序列
    Jzoj5236 利普希茨
    [置顶] 欢迎使用CSDN-markdown编辑器
    Jzoj5235 好的排列
    Jzoj5234 外星人的路径
    Jzoj5231 序列问题
  • 原文地址:https://www.cnblogs.com/davidwang456/p/2969479.html
Copyright © 2020-2023  润新知