您好,登錄后才能下訂單哦!
SpringFramework中ReflectiveMethodInvocation有什么用,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
Spring版本是5.0.4.release.
ReflectiveMethodInvocation是AOP中一個重要的類,這個類在JdkDynamicAopProxy的invoke方法中使用到它,如下的List-1
List-1
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { ... // We need to create a method invocation... invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); // Proceed to the joinpoint through the interceptor chain. retVal = invocation.proceed(); ... }
如下圖1所示
圖1
ReflectiveMethodInvocation實現(xiàn)了aop聯(lián)盟的MethodInvocation,間接實現(xiàn)了Invocation和Joinpoint。如下List-2所示,target是目標類,targetClass是目標類的class,method是目標方法,arguments是對應(yīng)的方法參數(shù),而interceptorsAndDynamicMethodMatchers則是對應(yīng)的攔截器。
List-2
protected ReflectiveMethodInvocation( Object proxy, @Nullable Object target, Method method, @Nullable Object[] arguments, @Nullable Class<?> targetClass, List<Object> interceptorsAndDynamicMethodMatchers) { this.proxy = proxy; this.target = target; this.targetClass = targetClass; this.method = BridgeMethodResolver.findBridgedMethod(method); this.arguments = AopProxyUtils.adaptArgumentsIfNecessary(method, arguments); this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers; }
ReflectiveMethodInvocation一個重要的方法是proceed(),如下List-3,currentInterceptorIndex表示訪問到第幾個攔截器,如果是最后一個,那么調(diào)用invokeJoinpoint(),如List-4所示,利用反射方式調(diào)用目標方法。
List-3
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); } }
List-3中,如果攔截器還沒有執(zhí)行完,則用遞歸的方式,調(diào)用下一個攔截器,
如果是InterceptorAndDynamicMethodMatcher,則獲取其MethodMatcher判斷是否match,如果match則調(diào)用其MethodInterceptor.
如果不是則直接調(diào)用MethodInterceptor的invoke方法,invoke方法中傳入this,會遞歸的調(diào)用下一個,這個和Spring security中的FilterProxy很相似??梢钥匆粋€MethodInterceptor的實現(xiàn)類AspectJAfterAdvice的實現(xiàn),如下List-5.
List-4
protected Object invokeJoinpoint() throws Throwable { return AopUtils.invokeJoinpointUsingReflection(this.target, this.method, this.arguments); } public static Object invokeJoinpointUsingReflection(@Nullable Object target, Method method, Object[] args) throws Throwable { ... ReflectionUtils.makeAccessible(method); return method.invoke(target, args); ... }
如下的List-5中,首先調(diào)用proceed(),之后才會執(zhí)行invokeAdviceMethod方法。
List-5
public class AspectJAfterAdvice extends AbstractAspectJAdvice implements MethodInterceptor, AfterAdvice, Serializable { ... @Override public Object invoke(MethodInvocation mi) throws Throwable { try { return mi.proceed(); } finally { invokeAdviceMethod(getJoinPointMatch(), null, null); } } ...
整體來說,就是Spring aop構(gòu)造一個攔截器鏈,在動態(tài)代理時調(diào)用,根據(jù)我們定義的aop,會在目標方法前后執(zhí)行。
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。