温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringFramework中ReflectiveMethodInvocation有什么用

发布时间:2021-06-22 14:38:57 来源:亿速云 阅读:829 作者:Leah 栏目:大数据

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所示

                             SpringFramework中ReflectiveMethodInvocation有什么用

                                                                                            图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中,如果拦截器还没有执行完,则用递归的方式,调用下一个拦截器,

  1. 如果是InterceptorAndDynamicMethodMatcher,则获取其MethodMatcher判断是否match,如果match则调用其MethodInterceptor.

  2. 如果不是则直接调用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,会在目标方法前后执行。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI