前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringFramework之ReflectiveMethodInvocation

SpringFramework之ReflectiveMethodInvocation

作者头像
克虏伯
发布2019-07-15 14:38:24
1.4K0
发布2019-07-15 14:38:24
举报

    Spring版本是5.0.4.release.

    ReflectiveMethodInvocation是AOP中一个重要的类,这个类在JdkDynamicAopProxy的invoke方法中使用到它,如下的List-1

List-1

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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,会在目标方法前后执行。

Reference

  1. https://github.com/spring-projects/spring-framework/tree/5.0.x

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reference
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档