Spring AOP 多個切點實現:JdkDynamicAopProxy

Spring Aop 的底層生成代理類i的實現除 jdk的動態代理技術外,還用到了Cglib,不過在封裝二者的設計原理上相差不大,只是底層工具不一樣而已。spring

本文只分析JdkDynamicAopProxy 是如何爲一個目標方法執行織入多個切點,也就是將本來可能須要多個「代理類「實現的業務放到一個代理類中(JdkDynamicAopProxy)完成。app

JdkDynamicAopProxy 自己就是一個JDK代理的InvocationHandler,spring 在調用其getProxy()方法返回一個代理類對象時:less

public Object getProxy(ClassLoader classLoader) {
        if (logger.isDebugEnabled()) {
            logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource());
        }
        Class[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised);
        findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
        return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
    }
Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); 傳入的 Handler 就是this,也就是 一個 JdkDynamicAopProxy 對象。因此代理的業務就在 JdkDynamicAopProxy
invoke()方法上。
 1 /**
 2      * Implementation of <code>InvocationHandler.invoke</code>.
 3      * <p>Callers will see exactly the exception thrown by the target,
 4      * unless a hook method throws an exception.
 5      */
 6     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 7         MethodInvocation invocation;
 8         Object oldProxy = null;
 9         boolean setProxyContext = false;
10 
11         TargetSource targetSource = this.advised.targetSource;
12         Class targetClass = null;
13         Object target = null;
14 
15         try {
16             if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
17                 // The target does not implement the equals(Object) method itself.
18                 return equals(args[0]);
19             }
20             if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
21                 // The target does not implement the hashCode() method itself.
22                 return hashCode();
23             }
24             if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
25                     method.getDeclaringClass().isAssignableFrom(Advised.class)) {
26                 // Service invocations on ProxyConfig with the proxy config...
27                 return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
28             }
29 
30             Object retVal;
31 
32             if (this.advised.exposeProxy) {
33                 // Make invocation available if necessary.
34                 oldProxy = AopContext.setCurrentProxy(proxy);
35                 setProxyContext = true;
36             }
37 
38             // May be null. Get as late as possible to minimize the time we "own" the target,
39             // in case it comes from a pool.
40             target = targetSource.getTarget();
41             if (target != null) {
42                 targetClass = target.getClass();
43             }
44 
45             // Get the interception chain for this method.
46            List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); 47 
48             // Check whether we have any advice. If we don't, we can fallback on direct
49             // reflective invocation of the target, and avoid creating a MethodInvocation.
50             if (chain.isEmpty()) {
51                 // We can skip creating a MethodInvocation: just invoke the target directly
52                 // Note that the final invoker must be an InvokerInterceptor so we know it does
53                 // nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
54                 retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args);
55             }
56             else {
57                 // We need to create a method invocation...
58                 invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); 59                 // Proceed to the joinpoint through the interceptor chain.
60                 retVal = invocation.proceed();
61             }
62 
63             // Massage return value if necessary.
64             if (retVal != null && retVal == target && method.getReturnType().isInstance(proxy) &&
65                     !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
66                 // Special case: it returned "this" and the return type of the method
67                 // is type-compatible. Note that we can't help if the target sets
68                 // a reference to itself in another returned object.
69                 retVal = proxy;
70             }
71             return retVal;
72         }
73         finally {
74             if (target != null && !targetSource.isStatic()) {
75                 // Must have come from TargetSource.
76                 targetSource.releaseTarget(target);
77             }
78             if (setProxyContext) {
79                 // Restore old proxy.
80                 AopContext.setCurrentProxy(oldProxy);
81             }
82         }
83     }
invoke方法中的紅色加粗代碼就是重點部分:

List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
該行代碼是構建代理鏈,獲取到目標方法須要加強的一系列業務代理對象。

invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
經過將目標方法和 多個業務代理對象 建立成一個
ReflectiveMethodInvocation,來實際完成 執行目標方法時執行一些攔截器,也就是代理業務。


retVal = invocation.proceed();
該行代碼開始執行代理業務和目標方法。

下面是
ReflectiveMethodInvocation 的 proceed方法代碼:
 1 public Object proceed() throws Throwable {
 2         //    We start with an index of -1 and increment early.
 3         if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
 4             return invokeJoinpoint();
 5         }
 6 
 7         Object interceptorOrInterceptionAdvice =
 8             this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
 9         if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
10             // Evaluate dynamic method matcher here: static part will already have
11             // been evaluated and found to match.
12             InterceptorAndDynamicMethodMatcher dm =
13                 (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
14             if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
15                 return dm.interceptor.invoke(this);
16             }
17             else {
18                 // Dynamic matching failed.
19                 // Skip this interceptor and invoke the next in the chain.
20                 return proceed(); 21             }
22         }
23         else {
24             // It's an interceptor, so we just invoke it: The pointcut will have
25             // been evaluated statically before this object was constructed.
26             return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
27         }
28     }

能夠看到proceed()方法經過從interceptorsAndDynamicMethodMatchers(list)集合中取出攔截器,攔截器是在前面建立ReflectiveMethodInvocation對象時傳入的chain,工具

經過判斷攔截器集合中全部攔截器是否執行完,未執行完這遞歸調用proceed()方法,執行完則執行目標方法。this

 

以上就是JdkDynamicAopProxy實現多個攔截器攔截目標方法的動態代理業務。lua

相關文章
相關標籤/搜索