zoukankan      html  css  js  c++  java
  • Spring——模拟实现动态代理类

    动态代理有两种实现方式:

    1. 基于接口实现动态代理:JDK动态代理

    2. 基于继承实现动态代理:Cglib、Javassist动态代理(尚且不会)

    本文目标:动态代理一个计算器功能

    第一步:创建接口 ArithmeticCalculator.java:

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

    第二步:创建要被代理的类的具体实现 ArithmeticCalculatorImpl.java:

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

    第三步:生成代理对象:

    ArithmeticCalculatorProxy.java

    实现动态代理需要清楚三个问题:

      目标对象;

      如何获取代理对象;

      代理要做什么

    //目标对象
        private ArithmeticCalculator target;
    
        public ArithmeticCalculatorProxy(ArithmeticCalculator target) {
            this.target = target;
        }

    获取代理对象的方法:

      在获取代理对象时,采用的方法是调用Proxy类的newProxyInstance方法: 

     public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
    ClassLoader loader = target.getClass().getClassLoader();
    Class[] interfaces = target.getClass().getInterfaces();
    InvocationHandler h的实现为匿名内部类:
    new InvocationHandler() {

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

    String methodName = method.getName();
    //记录日志
    System.out.println("LoggingProxy==> The method " + methodName + " begin with "+ Arrays.asList(args));
    Object result = method.invoke(target, args); //目标对象执行目标方法。相当于执行ArithmeticCalculatorImpl中的+-*/
    //记录日志
    System.out.println("LoggingProxy==> The method " + methodName + " ends with "+ result);
    return result;
    }
    }

    测试函数:
    
    

    运行结果:

    
    

    
    
    



     

     

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    EntityFramework 插入自增ID主从表数据
    API接收数据反序列化失败问题查找
    Linq Group by获取数量和数据
    git 常用命令
    Redis安装及局域网访问配置CentOS
    Azure虚拟机网站部署 防火墙设置
    .NET基础拾遗(1)类型语法基础和内存管理基础【转】
    ASP.NET CSRF 解决【网摘】
    win7 oracle 冷恢复
    $.ajax中contentType: “application/json” 的用法[转]
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/13854455.html
Copyright © 2011-2022 走看看