新聞中心
SpringFramework中ReflectiveMethodInvocation有什么用,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序設(shè)計(jì)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了汾西免費(fèi)建站歡迎大家使用!
Spring版本是5.0.4.release.
ReflectiveMethodInvocation是AOP中一個(gè)重要的類,這個(gè)類在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實(shí)現(xiàn)了aop聯(lián)盟的MethodInvocation,間接實(shí)現(xiàn)了Invocation和Joinpoint。如下List-2所示,target是目標(biāo)類,targetClass是目標(biāo)類的class,method是目標(biāo)方法,arguments是對(duì)應(yīng)的方法參數(shù),而interceptorsAndDynamicMethodMatchers則是對(duì)應(yīng)的攔截器。
List-2
protected ReflectiveMethodInvocation( Object proxy, @Nullable Object target, Method method, @Nullable Object[] arguments, @Nullable Class> targetClass, List
ReflectiveMethodInvocation一個(gè)重要的方法是proceed(),如下List-3,currentInterceptorIndex表示訪問(wèn)到第幾個(gè)攔截器,如果是最后一個(gè),那么調(diào)用invokeJoinpoint(),如List-4所示,利用反射方式調(diào)用目標(biā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中,如果攔截器還沒(méi)有執(zhí)行完,則用遞歸的方式,調(diào)用下一個(gè)攔截器,
如果是InterceptorAndDynamicMethodMatcher,則獲取其MethodMatcher判斷是否match,如果match則調(diào)用其MethodInterceptor.
如果不是則直接調(diào)用MethodInterceptor的invoke方法,invoke方法中傳入this,會(huì)遞歸的調(diào)用下一個(gè),這個(gè)和Spring security中的FilterProxy很相似??梢钥匆粋€(gè)MethodInterceptor的實(shí)現(xiàn)類AspectJAfterAdvice的實(shí)現(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(),之后才會(huì)執(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); } } ...
整體來(lái)說(shuō),就是Spring aop構(gòu)造一個(gè)攔截器鏈,在動(dòng)態(tài)代理時(shí)調(diào)用,根據(jù)我們定義的aop,會(huì)在目標(biāo)方法前后執(zhí)行。
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
本文標(biāo)題:SpringFramework中ReflectiveMethodInvocation有什么用
瀏覽地址:http://fisionsoft.com.cn/article/ghdhei.html