具体场景

  • 为了使代理类和被代理类对第三方有相同的函数,代理类和被代理类一般实现一个公共的interface,该interface定义如下

    public interface Calculator {
    public Integer add(Integer num1, Integer num2);
    public Integer minus(Integer num1, Integer num2);
    }
  • 被代理类定义如下
    public class CalculatorImpl implements Calculator {
    
        @Override
    public Integer add(Integer num1, Integer num2) {
    int ret = num1 + num2;
    System.out.println("in calculatorImpl, res: " + ret);
    return ret;
    } @Override
    public Integer minus(Integer num1, Integer num2) {
    int ret = num1 - num2;
    System.out.println("int calculatorImpl, res: " + ret);
    return ret;
    } }
  • 代理需求:在add函数和minus函数调用前后分别输出before invocation和after invocation字样

静态代理解决方案

  • 代码如下:简单直接,无需赘言,如果calculator里边不仅有add和minus,还有divide,product,log,sin…呢,呵呵哒

    public class StaticCalculatorProxy implements Calculator {
    Calculator obj; public StaticCalculatorProxy(Calculator obj) {
    this.obj = obj;
    } @Override
    public Integer add(Integer num1, Integer num2) {
    System.out.println("in StaticCalculatorProxy, before invocation");
    Integer ret = obj.add(num1, num2);
    System.out.println("in StaticCalculatorProxy, after invocation");
    return ret;
    } @Override
    public Integer minus(Integer num1, Integer num2) {
    System.out.println("in StaticCalculatorProxy, before invocation");
    Integer ret = obj.minus(num1, num2);
    System.out.println("in StaticCalculatorProxy, after invocation");
    return ret;
    } }

动态代理解决方案

  • 首先编写实现InvocationHandler接口的类,用于请求转发,实现如下

    public class CalculatorHandler implements InvocationHandler {
    
        private Object obj; //被代理类
    
        public CalculatorHandler(Object obj) {
    this.obj = obj;
    } @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("in calculatorhandler, before invocation"); Object ret = method.invoke(obj, args); //执行被代理类方法 System.out.println("in calculationhandler, after invocation");
    return ret;
    } }
  • 生成动态代理
    CalculatorImpl calculatorImpl = new CalculatorImpl();//被代理类
    CalculatorHandler calculatorHandler = new CalculatorHandler(calculatorImpl);
    Calculator calculator = (Calculator) Proxy.newProxyInstance(calculatorImpl.getClass().getClassLoader(), calculatorImpl.getClass().getInterfaces(), calculatorHandler);
    System.out.println(calculator.add(1,2));
    System.out.println(calculator.minus(1, 2));

    无论calculator中包含多少函数,动态代理只需实现一次,实际工程中,System.out.println(“in calculatorhandler, before invocation”)可能是加缓存,打日志等操作

动态代理工作原理

  • 为了搞清楚动态代理如何工作,首先看看生成的动态代理的代码是什么,借助[1]中ProxyUtil代码

    public class ProxyUtils {
    
        /**
    * Save proxy class to path
    *
    * @param path path to save proxy class
    * @param proxyClassName name of proxy class
    * @param interfaces interfaces of proxy class
    * @return
    */
    public static boolean saveProxyClass(String path, String proxyClassName, Class[] interfaces) {
    if (proxyClassName == null || path == null) {
    return false;
    } // get byte of proxy class
    byte[] classFile = ProxyGenerator.generateProxyClass(proxyClassName, interfaces);
    FileOutputStream out = null;
    try {
    out = new FileOutputStream(path);
    out.write(classFile);
    out.flush();
    return true;
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return false;
    }
    }
  • 得到了生成的动态代理代码如下:
    public final class $Proxy0 extends Proxy
    implements Calculator
    { public $Proxy0(InvocationHandler invocationhandler)
    {
    super(invocationhandler);
    } public final boolean equals(Object obj)
    {
    try
    {
    return ((Boolean)super.h.invoke(this, m1, new Object[] {
    obj
    })).booleanValue();
    }
    catch(Error _ex) { }
    catch(Throwable throwable)
    {
    throw new UndeclaredThrowableException(throwable);
    }
    } public final String toString()
    {
    try
    {
    return (String)super.h.invoke(this, m2, null);
    }
    catch(Error _ex) { }
    catch(Throwable throwable)
    {
    throw new UndeclaredThrowableException(throwable);
    }
    } public final Integer minus(Integer integer, Integer integer1)
    {
    try
    {
    return (Integer)super.h.invoke(this, m4, new Object[] {
    integer, integer1
    });
    }
    catch(Error _ex) { }
    catch(Throwable throwable)
    {
    throw new UndeclaredThrowableException(throwable);
    }
    } public final Integer add(Integer integer, Integer integer1)
    {
    try
    {
    return (Integer)super.h.invoke(this, m3, new Object[] {
    integer, integer1
    });
    }
    catch(Error _ex) { }
    catch(Throwable throwable)
    {
    throw new UndeclaredThrowableException(throwable);
    }
    } public final int hashCode()
    {
    try
    {
    return ((Integer)super.h.invoke(this, m0, null)).intValue();
    }
    catch(Error _ex) { }
    catch(Throwable throwable)
    {
    throw new UndeclaredThrowableException(throwable);
    }
    } private static Method m1;
    private static Method m2;
    private static Method m4;
    private static Method m3;
    private static Method m0; static
    {
    try
    {
    m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] {
    Class.forName("java.lang.Object")
    });
    m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
    m4 = Class.forName("com.langrx.mq.Calculator").getMethod("minus", new Class[] {
    Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
    });
    m3 = Class.forName("com.langrx.mq.Calculator").getMethod("add", new Class[] {
    Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
    });
    m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
    }
    catch(NoSuchMethodException nosuchmethodexception)
    {
    throw new NoSuchMethodError(nosuchmethodexception.getMessage());
    }
    catch(ClassNotFoundException classnotfoundexception)
    {
    throw new NoClassDefFoundError(classnotfoundexception.getMessage());
    }
    }
    }
  • 有点长,按照初始化顺序慢慢来分析,首先分析静态代码块:
    m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] {
    Class.forName("java.lang.Object")
    });
    m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
    m4 = Class.forName("com.langrx.mq.Calculator").getMethod("minus", new Class[] {
    Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
    });
    m3 = Class.forName("com.langrx.mq.Calculator").getMethod("add", new Class[] {
    Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
    });
    m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);

    得到公共interface中的add函数和minus函数对应的Method方法,同事也得到了equals,toString,hashCode三个函数的Method,所以调用代理类的equals,toString,hashCode也是要执行被代理类的方法的,知道这点很有必要

  • 构造函数
    public $Proxy0(InvocationHandler invocationhandler)
    {
    super(invocationhandler);
    }

    初始化了内部的InvocationHandler变量,也就是下文的super.h

  • 以add为例看一下请求的转发
    public final Integer add(Integer integer, Integer integer1)
    {
    try
    {
    return (Integer)super.h.invoke(this, m3, new Object[] {
    integer, integer1
    });
    }
    catch(Error _ex) { }
    catch(Throwable throwable)
    {
    throw new UndeclaredThrowableException(throwable);
    }
    }

    super.h.invoke就是invocationhandler.invoke就是传入的CalculatorHandler中实现的

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("in calculatorhandler, before invocation"); Object ret = method.invoke(obj, args); //执行被代理类方法 System.out.println("in calculationhandler, after invocation");
    return ret;
    }

    最终执行的就是CalculatorHandler对应的invoke函数

总结

Calculator calculator = (Calculator) Proxy.newProxyInstance(calculatorImpl.getClass().getClassLoader(), calculatorImpl.getClass().getInterfaces(), calculatorHandler);

生成动态代理的过程步骤如下[2]:

// InvocationHandlerImpl 实现了 InvocationHandler 接口,并能实现方法调用从代理类到委托类的分派转发
// 其内部通常包含指向委托类实例的引用,用于真正执行分派转发过来的方法调用
InvocationHandler handler = new InvocationHandlerImpl(..); // 通过 Proxy 为包括 Interface 接口在内的一组接口动态创建代理类的类对象
Class clazz = Proxy.getProxyClass(classLoader, new Class[] { Interface.class, ... }); // 通过反射从生成的类对象获得构造函数对象
Constructor constructor = clazz.getConstructor(new Class[] { InvocationHandler.class }); // 通过构造函数对象创建动态代理类实例
Interface Proxy = (Interface)constructor.newInstance(new Object[] { handler });

Proxy.newProxyInstance帮我们做了2,3,4步,直接返回给我们一个动态代理对象,代理对象最终执行InvocationHandler中invoke函数。顺便强推文章[2]

References

  1. Java 动态代理机制分析及扩展
  2. Java Dynamic Proxy Demo

JAVA静态&动态代理的更多相关文章

  1. Java特性-动态代理

    代理在开发中无处不在: 我们完成一个接口开发A,接口下有很多个实现类,这些类有些共同要处理的部分,比如每一个类都定义了接口A中的方法getXX(String name).我现在想把每次调用某个实现类的 ...

  2. java --- 设计模式 --- 动态代理

    Java设计模式——动态代理 java提供了动态代理的对象,本文主要探究它的实现, 动态代理是AOP(面向切面编程, Aspect Oriented Programming)的基础实现方式, 动态代理 ...

  3. [转载] java的动态代理机制详解

    转载自http://www.cnblogs.com/xiaoluo501395377/p/3383130.html 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代 ...

  4. 深入理解Java反射+动态代理

    答:   反射机制的定义: 是在运行状态中,对于任意的一个类,都能够知道这个类的所有属性和方法,对任意一个对象都能够通过反射机制调用一个类的任意方法,这种动态获取类信息及动态调用类对象方法的功能称为j ...

  5. JAVA设计模式-动态代理(Proxy)示例及说明

    在Mybatis源码解析,一步一步从浅入深(五):mapper节点的解析文章的最后部分,我们提到了动态代理的概念,下面我们就简单了解一下动态代理. 一,概念 代理设计模式的目的就是在不直接操作对象的前 ...

  6. JAVA设计模式-动态代理(Proxy)源码分析

    在文章:JAVA设计模式-动态代理(Proxy)示例及说明中,为动态代理设计模式举了一个小小的例子,那么这篇文章就来分析一下源码的实现. 一,Proxy.newProxyInstance方法 @Cal ...

  7. java的动态代理机制详解

    在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...

  8. java中动态代理实现机制

    前言: 代理模式是常用的java设计模式,它的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类之间通常会存在关联关系 ...

  9. java的动态代理机制

    前几天看到java的动态代理机制,不知道是啥玩意,然后看了看.死活不知道 invoke(Object proxy, Method m, Object[] args)种的proxy是个什么东西,放在这里 ...

随机推荐

  1. Python学习之旅(二十四)

    Python基础知识(23):进程和线程(Ⅱ) 一.threadlocal 在多线程环境下,每个线程都有自己的数据 一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响 ...

  2. 51nod图论题解(4级,5级算法题)

    51nod图论题解(4级,5级算法题) 1805 小树 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 她发现她的树的点上都有一个标号(从1到n),这些树都在空 ...

  3. iTextSharpH

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 使用FreeMarker生成word文档

    生成word文档的框架比较多,比如poi,java2word,itext和freemarker. 调研之后,freemarker来实现挺简单的,具体步骤如下: 1. 新建word文档,占位符用${}, ...

  5. [Day8] eclipse

    快捷键 1.内容辅助键  Alt+/ 2.格式化Ctrl+Shift+f 代码区域右键 -- Source – Format 3. 自动导包: Ctrl+Shift+o 如果当前类在多个包中都存在,这 ...

  6. C++11 vector使用emplace_back代替push_back

    C++11中,针对顺序容器(如vector.deque.list),新标准引入了三个新成员:emplace_front.emplace和emplace_back,这些操作构造而不是拷贝元素.这些操作分 ...

  7. mybatis+oracle实现简单的模糊查询

    第一种 concat select * from cat_table where cat_name like concat(#{catName},'%') --单个百分号 select * from ...

  8. Spring MVC原理及配置详解

    Spring MVC概述: Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单.这些控制 ...

  9. MongoDB $关键字 关系比较符号 $lt $lte $gt $gte $ne

    关系比较符: 小于:$lt 小于或等于:$lte 大于:$gt 大于或等于:$gte 不等于:$ne 属于:$in 查询中常见的 等于 大于 小于 大于等于 小于等于 等于 : 在MongoDB中什么 ...

  10. 流控制、FlowControl

    这个选项基本上所有网卡都会有,但是叫法会有些差别,比如Realtek网卡叫做流控制,Intel网卡叫做流程控制,还有一些网卡选项干脆是英文的,叫做FlowControl,很多交换机上也有这个功能,也叫 ...