• 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();
            }
        }

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

  • 相关阅读:
    kvm系列之二:kvm日常管理
    kvm系列之一:构建kvm虚拟机(centos7)
    cobbler无人值守安装
    判断我们的服务器是物理机还是虚拟机
    kickstark无人值守安装
    找回密码之单用户模式
    rsync传输引起的网络延迟
    题解 P3628 【[APIO2010]特别行动队】
    题解 P3211 【[HNOI2011]XOR和路径】
    题解 POJ1094 【Sorting It All Out】
  • 原文地址:https://www.cnblogs.com/ll409546297/p/10114651.html
Copyright © 2020-2023  润新知