基于 JDK 的动态代理-佟刚老师《Spring4视频教程》学习笔记(16)

为什么要使用AOP。

使用 Java 之 JDK 的动态代理实现 AOP 。

1、接口类代码:

package com.liwei.aop;

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、业务实现类代码:

package com.liwei.aop;

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;
    }

}

3、动态代理类的代码实现:

package com.liwei.aop.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

import com.liwei.aop.ArithmeticCalculator;

public class ArithmeticCalculatorLoggingProxy {
    // 第 1 步: 添加要代理的对象作为 Field
    private ArithmeticCalculator target;

    // 第 2 步:使用构造方法或者 set 方法,将被代理的对象注入进来
    public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {
        super();
        this.target = target;
    }

    // 第 3 步:写一个方法返回一个代理对象(基于被代理对象)

    public ArithmeticCalculator getLoggingProxy(){
        ArithmeticCalculator proxy = null;


        InvocationHandler h = new InvocationHandler() {
            /**
             * proxy:代理的对象,一般我们在 invoke 方法里面都不使用。
             * method:正在被调用的方法。
             * args:正在被调用的方法传入的参数。
             */
            @Override
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
                Object result = null;
                System.out.println("正在执行的方法的名字:" + method.getName() + ",参数为:" + Arrays.asList(args));
                // 代表使用被代理对象(target),传进来参数,去执行,注意,这个方法根据不同的业务逻辑可能抛出异常
                try {
                    // 前置通知
                    // 因为方法可以能会出异常,所以访问不到方法的返回值
                    result = method.invoke(target, args);
                    // 执行到这里了就说明方法可以顺利执行
                    // 说明可以访问到方法的返回值
                    // 这里写的代码是返回通知

                } catch (Exception e) {
                    e.printStackTrace();
                    //异常通知,可以访问到方法出现的异常
                }
                // 后置通知,因为上面的 invoke 方法可以能会出异常,所以访问不到方法的返回值
                // 因为有前面的异常捕获机制,所以这行代码是一定会被执行到,正正好也符合了后置通知的特性:无论目标方法是否抛出异常都会得到执行

                System.out.println("正在执行的方法的返回值:" + result);
                return result;
            }
        };

        ClassLoader loader = target.getClass().getClassLoader();
        //Class [] interfaces = new Class[]{ArithmeticCalculator.class};
        Class<?>[] interfaces = target.getClass().getInterfaces();


        /**
         * loader: 代理对象使用的类加载器。
         * interfaces: 设置代理对象的类型, 即代理代理对象中可以有哪些方法。
         * h: 当具体调用代理对象的方法时, 应该如何进行响应, 实际上就是调用 InvocationHandler 的 invoke 方法。
         * 
         */
        proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader , interfaces , h);

        return proxy;

    }

}

4、测试类代码:

package com.liwei.aop.proxy;

import com.liwei.aop.ArithmeticCalculator;
import com.liwei.aop.ArithmeticCalculatorImpl;

public class TestProxy {
    public static void main(String[] args) {
        ArithmeticCalculator ac = new ArithmeticCalculatorLoggingProxy(
                new ArithmeticCalculatorImpl()).getLoggingProxy();
        ac.add(5, 6);
        ac.sub(15, 5);
        ac.mul(3, 4);
        ac.div(12, 6);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值