spring aop的前奏,动态代理 (5)

一、先看一个计算器的抽取和实现

  • 图中,有两个需求
    • 需求1-日志:在程序执行期间追踪正在发生的活动
    • 需求2-验证:希望计算器只能处理正数的运算
  • 其中实现的代码片段

从代码片段中可以看出,代码混乱,代码分散,如果日志需求变更,必须修改所有方法。

二、使用动态代理解决以上问题。

1 设计原理

  • 代理设计模式的原理: 使用一个代理将对象包装起来, 然后用该代理对象取代原始对象. 任何对原始对象的调用都要通过代理. 代理对象决定是否以及何时将方法调用转到原始对象上.

2 代码实现

2.1 接口代码

public interface ArithmeticCalculator {
    int add(int i, int j);
    int sub(int i, int j);
    
    int mul(int i, int j);
    int div(int i, int j);
}  

2.2 实现接口的代码

public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
    @Override
    public int add(int i, int j) {
        int result = i + j;
        return result;
    }
    @Override
    public int sub(int i, int j) {
        int result = i - j;
        return result;
    }
    @Override
    public int mul(int i, int j) {
        int result = i * j;
        return result;
    }
    @Override
    public int div(int i, int j) {
        int result = i / j;
        return result;
    }
}  

2.3 测试代码

    public static void main(String[] args) {
        ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorImpl();
        
        int result = arithmeticCalculator.add(11, 12);
        System.out.println("result:" + result);
        
        result = arithmeticCalculator.div(21, 3);
        System.out.println("result:" + result);
    }  

打印出
result:23
result:7
这个是原始的代码,接下来,我们加入动态代理

2.3 创建动态代理类

public class ArithmeticCalculatorLoggingProxy {
    // 要代理的对象
    private ArithmeticCalculator target;
    public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {
        super();
        this.target = target;
    }
    // 返回代理对象
    public ArithmeticCalculator getLoggingProxy() {
        ArithmeticCalculator proxy = null;
        ClassLoader loader = target.getClass().getClassLoader();
        Class[] interfaces = new Class[] { ArithmeticCalculator.class };
        InvocationHandler h = new InvocationHandler() {
            /**
             * proxy: 代理对象。 一般不使用该对象 method: 正在被调用的方法 args: 调用方法传入的参数
             */
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodName = method.getName();
                // 打印日志
                System.out.println("[before] The method " + methodName + " begins with " + Arrays.asList(args));
                // 调用目标方法
                Object result = null;
                try {
                    // 前置通知
                    result = method.invoke(target, args);
                    // 返回通知, 可以访问到方法的返回值
                } catch (NullPointerException e) {
                    e.printStackTrace();
                    // 异常通知, 可以访问到方法出现的异常
                }
                // 后置通知. 因为方法可以能会出异常, 所以访问不到方法的返回值
                // 打印日志
                System.out.println("[after] The method ends with " + result);
                return result;
            }
        };
        /**
         * loader: 代理对象使用的类加载器。 
         * interfaces: 指定代理对象的类型. 即代理代理对象中可以有哪些方法. 
         * h: 当具体调用代理对象的方法时, 应该如何进行响应, 实际上就是调用 InvocationHandler 的 invoke 方法
         */
        proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
        return proxy;
    }
}

2.3.1 测试动态代理

    public static void main(String[] args) {
        ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorImpl();
        
        arithmeticCalculator = 
                new ArithmeticCalculatorLoggingProxy(arithmeticCalculator).getLoggingProxy();
        
        int result = arithmeticCalculator.add(11, 12);
        System.out.println("result:" + result);
        
        result = arithmeticCalculator.div(21, 3);
        System.out.println("result:" + result);
    }  

打印出:
[before] The method add begins with [11, 12]
[after] The method ends with 23
result:23
[before] The method div begins with [21, 3]
[after] The method ends with 7
result:7

2.4 动态代理类代码说明

代理类中, Proxy.newProxyInstance(loader, interfaces, h); 这个类需要传递三个参数

  • loader: 代理对象使用的类加载器。使用目标方法的类加载器即可
  • interfaces: 指定代理对象的类型. 即代理代理对象中可以有哪些方法.
  • h: 当具体调用代理对象的方法时, 应该如何进行响应, 实际上就是调用 InvocationHandler 的 invoke 方法

传递的h中InvocationHandler 的参数介绍:

  • proxy: 代理对象。 一般不使用该对象
  • method: 正在被调用的方法,调用method.invoke(目标对象, args);
  • args: 调用方法传入的参数
    public Object invoke(Object proxy, Method method, Object[] args)

2.5 前置通知,后置通知,返回通知,异常通知

在invoke方法中,可以看出,在方法前后分别可以打印日志,发生异常时,也可以在catch中对异常的处理,这几个不同的位置,分别为 : 前置通知,后置通知,返回通知,异常通知

前置通知 : 调用目标方法之前执行的代码
后置通知 : 调用目标方法之后执行的代码
返回通知 : 可以得到目标方法返回值,从而进一步处理,在method.invoke(目标对象, args);之后
异常通知 : 调用目标方法出异常时,catch中执行的代码,此时,返回通知无法别执行

整个系列项目代码: http://git.oschina.net/nmc5/spring

posted @ 2016-09-18 15:30  hp柠檬茶  阅读(572)  评论(0编辑  收藏  举报