zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    dljd_(004_005)_jdbc编程步骤
    dljd_003_jdbc编程_概述
    dljd_002_通过接口降低代码的耦合度(2)
    dljd_001_通过接口降低代码的耦合度(1)
    dljd_(002-003)_什么是持久化
    dljd_001_由hibernate名称引出的相关知识
    001_学习26个英文字母
    06_dljd_mysql数据库常用操作
    05_dljd_mysql数据库表的介绍
    【数据结构】树状数组(简单名次树)
  • 原文地址:https://www.cnblogs.com/davidwang456/p/2969479.html
Copyright © 2011-2022 走看看