JAVA 中的 动态代理

发现JAVA 提供了 动态代理 的 默认实现(以前没用到过),

主要由 Proxy 类,InvocationHandler 接口(在 java.lang.reflect 包中)组成。

 

下面 演示一个使用 Proxy 和 InvocationHandler 类实现动态代理 的例子,

这个动态代理 对 被代理对象的方法执行时间进行计时,并将执行时间打印输出到控制台。

 

1,被代理类 的接口 Proxied

 

Java代码   收藏代码
  1. // 被代理类 需实现的 接口  
  2. public interface Proxied {  
  3.     void doSomething();  
  4.     void doSomethingElse(String str);  
  5. }  
 

2,一个 Proxied接口 的实现类(被代理类)

 

Java代码   收藏代码
  1. public class ConcreteProxied implements Proxied {  
  2.     @Override  
  3.     public void doSomething() {  
  4.         try {  
  5.             Thread.sleep(100);  
  6.         } catch (InterruptedException e) {  
  7.             System.err.println("Error : InterruptedException");  
  8.         }  
  9.         System.out.println(this.getClass().getSimpleName()  
  10.                 + " >> doSomething .");  
  11.     }  
  12.   
  13.     @Override  
  14.     public void doSomethingElse(String str) {  
  15.         try {  
  16.             Thread.sleep(150);  
  17.         } catch (InterruptedException e) {  
  18.             System.err.println("Error : InterruptedException");  
  19.         }  
  20.         System.out.println(this.getClass().getSimpleName()  
  21.                 + " >> doSomethingElse , argument = " + str + ".");  
  22.     }  
  23. }  
 

3,TimingInvocationHandler 类,实现了 InvocationHandler 接口

 

Java代码   收藏代码
  1. import java.lang.reflect.InvocationHandler;  
  2. import java.lang.reflect.Method;  
  3.   
  4. public class TimeingInvocationHandler implements InvocationHandler{  
  5.     //被代理的对象  
  6.     private Object proxied;  
  7.     public TimeingInvocationHandler(Object proxied){  
  8.         this.proxied = proxied;  
  9.     }  
  10.       
  11.     // 参数  proxy 表示代理类的对象  
  12.     // 参数  method 表示被代理类 和 代理类 都实现的接口 的方法对象  
  13.     // 参数  args 表示方法 method 的参数数组  
  14.     @Override  
  15.     public Object invoke(Object proxy, Method method, Object[] args)  
  16.             throws Throwable {  
  17.         System.out.println(method.getDeclaringClass().getName());  
  18.         long currentTimeMillis = System.currentTimeMillis();  
  19.         Object ret = method.invoke(proxied, args);  
  20.         System.out.println(this.getClass().getSimpleName()+" >> wastes time : "  
  21.                 +(System.currentTimeMillis() - currentTimeMillis)+"ms");  
  22.         return ret;  
  23.     }  
  24.   
  25. }  
 

4,测试类 Test

 

Java代码   收藏代码
  1. import java.lang.reflect.Proxy;  
  2.   
  3. public class TestProxy {  
  4.     public static void main(String[] args) {  
  5.         Proxied proxied = new ConcreteProxied();  
  6.         proxied.doSomething();  
  7.         proxied.doSomethingElse("only a String");  
  8.   
  9.         // 生成一个代理实例,这个代理实现了 Proxied 接口  
  10.         // 对这个代理(proxy)的方法的调用 会 重定向到 TimeingInvocationHandler 的 invoke 方法  
  11.         Proxied proxy = (Proxied) Proxy.newProxyInstance(Proxied.class  
  12.                 .getClassLoader(), // 类加载器  
  13.                 new Class[] { Proxied.class }, // 代理要实现的接口  
  14.                 new TimeingInvocationHandler(proxied) // 调用处理器  
  15.                 );  
  16.         proxy.doSomething();  
  17.         proxy.doSomethingElse("only a String");  
  18.     }  
  19.   
  20. }  

 

 运行Test类,输出如下:

 

ConcreteProxied >> doSomething .
ConcreteProxied >> doSomethingElse , argument = only a String.

ConcreteProxied >> doSomething .
TimeingInvocationHandler >> wastes time : 93ms
ConcreteProxied >> doSomethingElse , argument = only a String.
TimeingInvocationHandler >> wastes time : 157ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值