• spring源码-aop增强-5.2


      一、aop增强就是针对于不同的切面进行的相关增强,目的当然是更好的支持相关应用和解耦。

      二、默认的aop增强类有AspectJMethodBeforeAdvice、AspectJMethodBeforeAdvice、AspectJAfterReturningAdvice、AspectJAfterThrowingAdvice、AspectJAroundAdvice。

      三、这里讲2个增强AspectJMethodBeforeAdvice、AspectJMethodBeforeAdvice(为什么是两个,因为这个两个有点差异)

      四、增强源码:

      1)AspectJMethodBeforeAdvice

      

      从结构上面来说是通过MethodBeforeAdvicebefore()方法进行执行的。

    public void before(Method method, Object[] args, Object target) throws Throwable {
            //默认的执行方式
            this.invokeAdviceMethod(this.getJoinPointMatch(), (Object)null, (Throwable)null);
        }
    
        protected Object invokeAdviceMethod(JoinPointMatch jpMatch, Object returnValue, Throwable ex) throws Throwable {
            return this.invokeAdviceMethodWithGivenArgs(this.argBinding(this.getJoinPoint(), jpMatch, returnValue, ex));
        }
    
        protected Object invokeAdviceMethodWithGivenArgs(Object[] args) throws Throwable {
            Object[] actualArgs = args;
            if (this.aspectJAdviceMethod.getParameterTypes().length == 0) {
                actualArgs = (Object[])null;
            }
    
            try {
                ReflectionUtils.makeAccessible(this.aspectJAdviceMethod);
                //这里就是具体调用过程了,method.invoke(object, ...agrs)
                return this.aspectJAdviceMethod.invoke(this.aspectInstanceFactory.getAspectInstance(), actualArgs);
            } catch (IllegalArgumentException var4) {
                throw new AopInvocationException("Mismatch on arguments to advice method [" + this.aspectJAdviceMethod + "]; pointcut expression [" + this.pointcut.getPointcutExpression() + "]", var4);
            } catch (InvocationTargetException var5) {
                throw var5.getTargetException();
            }
        }

      2)AspectJAfterAdvice

      

      发现什么不一样了吧,这里AspectJAfterAdvice使用的是MethodInterceptor的方法拦截方式,也就是在MethodInterceptor中的invoke()作为调用过程.

    public Object invoke(MethodInvocation mi) throws Throwable {
            Object var3;
            try {
                var3 = mi.proceed();
            } finally {
                //实际调用
                this.invokeAdviceMethod(this.getJoinPointMatch(), (Object)null, (Throwable)null);
            }
    
            return var3;
        }
    
        protected Object invokeAdviceMethod(JoinPointMatch jpMatch, Object returnValue, Throwable ex) throws Throwable {
            return this.invokeAdviceMethodWithGivenArgs(this.argBinding(this.getJoinPoint(), jpMatch, returnValue, ex));
        }
    
        protected Object invokeAdviceMethodWithGivenArgs(Object[] args) throws Throwable {
            Object[] actualArgs = args;
            if (this.aspectJAdviceMethod.getParameterTypes().length == 0) {
                actualArgs = (Object[])null;
            }
    
            try {
                ReflectionUtils.makeAccessible(this.aspectJAdviceMethod);
                //执行方法的过程method.invoke(object, ...agrs)
                return this.aspectJAdviceMethod.invoke(this.aspectInstanceFactory.getAspectInstance(), actualArgs);
            } catch (IllegalArgumentException var4) {
                throw new AopInvocationException("Mismatch on arguments to advice method [" + this.aspectJAdviceMethod + "]; pointcut expression [" + this.pointcut.getPointcutExpression() + "]", var4);
            } catch (InvocationTargetException var5) {
                throw var5.getTargetException();
            }
        }

      五、这基本就是增强器的调用过程了,但是有点需要明确。增强的东西实在动态代理加入进去的,实际调用才会执行!

  • 相关阅读:
    selenium
    python第三方模块的安装
    程序员学习网站
    python 数据较大 性能分析
    linux ~/ 和 /
    VMWare虚拟机 window文件传递
    vi命令
    os.system
    win10系统进入BIOS
    pyinstaller将python脚本生成exe
  • 原文地址:https://www.cnblogs.com/ll409546297/p/10114651.html
Copyright © 2020-2023  润新知