您好,登錄后才能下訂單哦!
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實現了aop聯盟的MethodInvocation,間接實現了Invocation和Joinpoint。如下List-2所示,target是目標類,targetClass是目標類的class,method是目標方法,arguments是對應的方法參數,而interceptorsAndDynamicMethodMatchers則是對應的攔截器。
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表示訪問到第幾個攔截器,如果是最后一個,那么調用invokeJoinpoint(),如List-4所示,利用反射方式調用目標方法。
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中,如果攔截器還沒有執行完,則用遞歸的方式,調用下一個攔截器,
如果是InterceptorAndDynamicMethodMatcher,則獲取其MethodMatcher判斷是否match,如果match則調用其MethodInterceptor.
如果不是則直接調用MethodInterceptor的invoke方法,invoke方法中傳入this,會遞歸的調用下一個,這個和Spring security中的FilterProxy很相似。可以看一個MethodInterceptor的實現類AspectJAfterAdvice的實現,如下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中,首先調用proceed(),之后才會執行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構造一個攔截器鏈,在動態代理時調用,根據我們定義的aop,會在目標方法前后執行。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。