深度解析Spring5源码

28--Aop知识点回顾以及基于Advice接口的增强实现

2018-11-05  本文已影响71人  闲来也无事

上一章节分析了静态代理和JDK、CGLIB动态代理,接下来我们还要回顾一下AOP的一些相关知识,以方便为接下来分析AOP的源码做好准备。

1.先来回顾一下AOP中的一些术语。

上面所提到的概念,比较抽象,也比较枯燥,而且在实际的开发中使用切面编程只占很少一部分,但是如果大家对上面的概念无所了解的话,那么对接下来的源码分析必然是一头雾水,下面我们再通过几个例子,让大家对上面的概念有所了解,为源码分析做好准备。

2. 增强方式简介

也有很多人将增强理解为通知,但是理解为增强会更加准确,因为它表示在连接点上执行的行为,这个行为是目标类类所没有的,是为目标类增加了额外的方法或者其他的一些功能,所以理解为增强比通知更加贴切,当然如果有的人已经习惯了通知这个概念的话也无所谓,只要知道本文将的增强即是通知即可。

增强的类型有前置增强、后置返回增强、异常增强、环绕增强、引介增强、后置最终增强等。下面我们通过实例的例子来介绍一下,为了让大家能够更为深刻的理解SpringAop,我们不会一开始就讲解基于@AspectJ或者基于Schema配置文件的方式,而是从最基础开始讲解,毕竟AOP在实际开发中并不占太大的比重,相信很多人并没有深刻的理解。

接下来我们先讲解基于Advice接口以编码方式实现的增强。

3.MethodBeforeAdvice前置增强
package com.lyc.cn.v2.day04.advisor;

/**
 * @author: LiYanChao
 * @create: 2018-11-01 21:48
 */
public interface Animal {
    void sayHello(String name,int age);
    void sayException(String name,int age);
}
package com.lyc.cn.v2.day04.advisor;

/**
 * @author: LiYanChao
 * @create: 2018-11-01 21:48
 */
public class Dog implements Animal {

    @Override
    public void sayHello(String name, int age) {
        System.out.println("==名字:" + name + " 年龄:" + age);
    }

    @Override
    public void sayException(String name, int age) {
        System.out.println("==名字:" + name + " 年龄:" + age);
        System.out.println("==抛出异常:" + 1 / 0);
    }
}
package com.lyc.cn.v2.day04.advisor;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;

/**
 * 前置增强
 * @author: LiYanChao
 * @create: 2018-11-01 21:50
 */
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
    /**
     * Callback before a given method is invoked.
     * @param method method being invoked
     * @param args   arguments to the method
     * @param target target of the method invocation. May be {@code null}.
     * @throws Throwable if this object wishes to abort the call.
     *                   Any exception thrown will be returned to the caller if it's
     *                   allowed by the method signature. Otherwise the exception
     *                   will be wrapped as a runtime exception.
     */
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("==前置增强");
        System.out.println("==方法名:" + method.getName());
        if (null != args && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                System.out.println("==第" + (i + 1) + "参数:" + args[i]);
            }
        }
        System.out.println("==目标类信息:" + target.toString());
    }
}
4.AfterReturningAdvice后置增强
package com.lyc.cn.v2.day04.advisor;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;

/**
 * 后置增强
 * @author: LiYanChao
 * @create: 2018-11-01 22:09
 */
public class MyAfterReturningAdvice implements AfterReturningAdvice {
    /**
     * Callback after a given method successfully returned.
     * @param returnValue the value returned by the method, if any
     * @param method      method being invoked
     * @param args        arguments to the method
     * @param target      target of the method invocation. May be {@code null}.
     * @throws Throwable if this object wishes to abort the call.
     *                   Any exception thrown will be returned to the caller if it's
     *                   allowed by the method signature. Otherwise the exception
     *                   will be wrapped as a runtime exception.
     */
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("==后置增强");
        System.out.println("==方法名:" + method.getName());
        if (null != args && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                System.out.println("==第" + (i + 1) + "参数:" + args[i]);
            }
        }
        System.out.println("==目标类信息:" + target.toString());
    }
}
5.ThrowsAdvice异常增强
package com.lyc.cn.v2.day04.advisor;
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;

/**
 * @author: LiYanChao
 * @create: 2018-11-01 22:17
 */
public class MyThrowsAdvice implements ThrowsAdvice {

    /**
     * 异常增强
     */
    public void afterThrowing(Method method, Object[] args, Object target, Exception ex) {
        System.out.println("==异常增强");
        System.out.println("==方法名:" + method.getName());
        if (null != args && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                System.out.println("==第" + (i + 1) + "参数:" + args[i]);
            }
        }
        System.out.println("==目标类信息:" + target.toString());
        System.out.println("==异常信息:" + ex.toString());
    }
}
6.MethodInterceptor环绕增强
package com.lyc.cn.v2.day04.advisor;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * 环绕增强
 * @author: LiYanChao
 * @create: 2018-11-01 22:29
 */
public class MyMethodInterceptor implements MethodInterceptor {

    /**
     * 环绕增强 这里的方法参数与之前的前置增强、后置增强明显不同,只有一个MethodInvocation类型的参数
     * Implement this method to perform extra treatments before and
     * after the invocation. Polite implementations would certainly
     * like to invoke {@link Joinpoint#proceed()}.
     * @param invocation the method invocation joinpoint
     * @return the result of the call to {@link Joinpoint#proceed()};
     * might be intercepted by the interceptor
     * @throws Throwable if the interceptors or the target object
     *                   throws an exception
     */
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("==环绕增强开始");
        System.out.println("==方法名:" + invocation.getMethod().getName());
        Object[] args = invocation.getArguments();
        if (null != args && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                System.out.println("==第" + (i + 1) + "参数:" + args[i]);
            }
        }

        Object proceed = invocation.proceed();

        System.out.println("==环绕增强结束");
        return proceed;
    }
}
7.测试及结果
@Test
public void test5() {
    // 前置增强
    // 1、实例化bean和增强
    Animal dog = new Dog();
    MyMethodBeforeAdvice advice = new MyMethodBeforeAdvice();

    // 2、创建ProxyFactory并设置代理目标和增强
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTarget(dog);
    proxyFactory.addAdvice(advice);

    // 3、生成代理实例
    Animal proxyDog = (Animal) proxyFactory.getProxy();
    proxyDog.sayHello("二哈", 3);
}


@Test
public void test6() {
    // 后置增强
    // 1、实例化bean和增强
    Animal dog = new Dog();
    MyAfterReturningAdvice advice = new MyAfterReturningAdvice();

    // 2、创建ProxyFactory并设置代理目标和增强
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTarget(dog);
    proxyFactory.addAdvice(advice);

    // 3、生成代理实例
    Animal proxyDog = (Animal) proxyFactory.getProxy();
    proxyDog.sayHello("二哈", 3);

}

@Test
public void test7() {
    // 异常增强
    // 1、实例化bean和增强
    Animal dog = new Dog();
    MyThrowsAdvice advice = new MyThrowsAdvice();

    // 2、创建ProxyFactory并设置代理目标和增强
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTarget(dog);
    proxyFactory.addAdvice(advice);

    // 3、生成代理实例
    Animal proxyDog = (Animal) proxyFactory.getProxy();
    proxyDog.sayException("二哈", 3);

}


@Test
public void test8() {
    // 环绕增强
    // 1、实例化bean和增强
    Animal dog = new Dog();
    MyMethodInterceptor advice = new MyMethodInterceptor();

    // 2、创建ProxyFactory并设置代理目标和增强
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTarget(dog);
    proxyFactory.addAdvice(advice);

    // 3、生成代理实例
    Animal proxyDog = (Animal) proxyFactory.getProxy();
    proxyDog.sayHello("二哈", 3);

}
信息: Creating CGLIB proxy: target source is SingletonTargetSource for target object [com.lyc.cn.v2.day04.advisor.Dog@2280cdac]
==前置增强
==方法名:sayHello
==第1参数:二哈
==第2参数:3
==目标类信息:com.lyc.cn.v2.day04.advisor.Dog@2280cdac
==名字:二哈 年龄:3
十一月 03, 2018 10:32:46 下午 org.springframework.aop.framework.CglibAopProxy getProxy
信息: Creating CGLIB proxy: target source is SingletonTargetSource for target object [com.lyc.cn.v2.day04.advisor.Dog@6b2fad11]
==名字:二哈 年龄:3
==后置增强
==方法名:sayHello
==第1参数:二哈
==第2参数:3
==目标类信息:com.lyc.cn.v2.day04.advisor.Dog@6b2fad11
十一月 03, 2018 10:32:46 下午 org.springframework.aop.framework.CglibAopProxy getProxy
信息: Creating CGLIB proxy: target source is SingletonTargetSource for target object [com.lyc.cn.v2.day04.advisor.Dog@38082d64]
==名字:二哈 年龄:3
==异常增强
==方法名:sayException
==第1参数:二哈
==第2参数:3
==目标类信息:com.lyc.cn.v2.day04.advisor.Dog@38082d64
==异常信息:java.lang.ArithmeticException: / by zero

十一月 03, 2018 10:32:46 下午 org.springframework.aop.framework.CglibAopProxy getProxy
信息: Creating CGLIB proxy: target source is SingletonTargetSource for target object [com.lyc.cn.v2.day04.advisor.Dog@3f2a3a5]

java.lang.ArithmeticException: / by zero

    at com.lyc.cn.v2.day04.advisor.Dog.sayException(Dog.java:17)
    at com.lyc.cn.v2.day04.advisor.Dog$$FastClassBySpringCGLIB$$a974b1ec.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
==环绕增强开始
==方法名:sayHello
==第1参数:二哈
==第2参数:3
==名字:二哈 年龄:3
==环绕增强结束
8.总结

以上简单介绍了前置增强、后置增强、环绕增强、异常增强等以编码实现的方式,当然以上实现也可以通过配置文件的方式实现。本篇所介绍的知识点比较简单,但是理解增强的概念是AOP的基础,其实本篇的各种增强方式用上一篇讲解的动态代理是完全可以实现的,因为本篇使用的代理工厂ProxyFactory内部使用的是CglibAopProxyJdkDynamicAopProxy,其实其本质还是JDK或CGLIB动态代理,在接下来的章节中会详细讲解CglibAopProxyJdkDynamicAopProxy的实现,本篇的分析就到这里了。

上一篇 下一篇

猜你喜欢

热点阅读