java动态代理

本文详细解析了Java中动态代理的工作原理,对比静态代理,展示了如何利用InvocationHandler实现方法调用的转发,以及动态代理类的生成过程。

http://www.importnew.com/26166.html


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

1
2
3
4
public interface Calculator {
     public Integer add(Integer num1, Integer num2);
     public Integer minus(Integer num1, Integer num2);
}

被代理类定义如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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…呢,呵呵哒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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接口的类,用于请求转发,实现如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
     }
 
}

生成动态代理

1
2
3
4
5
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代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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 ;
     }
}

得到了生成的动态代理代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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());
         }
     }
}

有点长,按照初始化顺序慢慢来分析,首先分析静态代码块:

1
2
3
4
5
6
7
8
9
10
11
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也是要执行被代理类的方法的,知道这点很有必要

构造函数

1
2
3
4
public $Proxy0(InvocationHandler invocationhandler)
{
         super (invocationhandler);
}

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

以add为例看一下请求的转发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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中实现的

1
2
3
4
5
6
7
8
9
@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函数

总结

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

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

1
2
3
4
5
6
7
8
9
10
11
12
// 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]


豌豆代理(又称豌豆 IP)是一款一站式国内代理 IP 服务平台,主打高匿名、低延迟、高可用的 IP 资源,支持 HTTP/HTTPS/SOCKS5 协议,适配 Windows、Mac、Android、iOS 多平台。 多类型 IP 资源与高覆盖节点 提供动态住宅 IP、静态独享 IP、数据中心 IP,覆盖全国 200 + 城市,可用率 99%+;支持省市精准选择或全国混拨,适配不同业务合规与稳定性需求。 使用:在客户端 “节点 / 线路” 页,按城市 / 类型筛选,一键连接目标 IP,适合爬虫、电商多账号运营等场景。 秒级 IP 切换与灵活调度 支持手动一键切换、秒级动态切换(切换速度低至 100ms)、定时切换(自定义时长),并自动过滤重复 IP,避免重复使用导致风险。 使用:在 “设置” 中开启 “自动切换” 并设时间间隔,或按 Ctrl+Q 快捷键一键换 IP,适配反爬虫、批量测试等高频切换场景。 全协议支持与多端适配 兼容 HTTP/HTTPS/SOCKS5 主流代理协议,可对接浏览器、爬虫脚本、客户端软件;支持 Windows、Mac、安卓、iOS 多端同步使用,跨设备无缝切换。 使用:在客户端 “协议设置” 选择对应协议,生成代理地址与端口,直接填入目标软件即可生效。 隐私安全与数据加密 自研传输加密技术保护数据传输,搭配高匿名 IP 隐藏真实地址,同时支持自动清除 Cookie / 缓存,降低隐私泄露与追踪风险。 使用:在 “安全设置” 中开启 “数据加密” 与 “自动清理缓存”,公共 WiFi 环境下优先启用,提升隐私防护等级。 智能筛选与稳定网络优化 系统自动筛选低延迟、高可用 IP,过滤失效 / 重复地址;依托自建纯净机房与独享带宽,搭配 BGP 多线接入,保障连接稳定性与速度。 使用:无需手动配置,客户端默认智能匹配合适节点,复杂网络环境可在 “网络
在网络高速发展的时代,众多的软件被开发出来,给用户带来了很大的选择余地,而且人们越来越追求更个性的需求。在这种时代背景下,商家只能以用户为导向,以商品的持续创新作为商家最重要的事项。 在新发展的时代,人们对幼儿资源互助共享平台越来越重视,才能实现幼儿资源互助共享平台的有效发挥,本文将通过幼儿资源互助共享平台的信息,分析在日常生活中对幼儿资源互助共享平台存在哪些问题探讨出进一步提升效率,管理能力的对策。 系统采用了Java技术,将所有模块采用以浏览器交互的模式,选择MySQL作为系统的数据库,来进行系统的设计。基本实现了幼儿资源互助共享平台应有的主要功能模块,本系统有管理员:首页、个人中心、用户管理、卖家管理、咨询师管理、萌宝信息管理、幼儿知识管理、保姆推荐管理、音频资源管理、二手商品管理、商品分类管理、资源分类管理、交流论坛、系统管理,用户;首页、个人中心、萌宝信息管理、保姆推荐管理、音频资源管理,卖家;首页、个人中心、二手商品管理、订单管理,咨询师;首页、个人中心、幼儿知识管理,前台首页;首页、萌宝信息、幼儿知识、保姆推荐、音频资源、二手商品、交流论坛、个人中心、后台管理、购物车等功能。 对系统进行测试后,改善了程序逻辑和代码。同时确保系统中所有的程序都能正常运行,所有的功能都能操作,本系统的开发获取幼儿资源互助共享平台信息能够更加方便快捷,同时也使幼儿资源互助共享平台信息变的更加系统化、有序化。系统界面较友好,易于操作。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值