java 高新技术【11.1】 动态代理类 InvocationHandler对象


1,客户端 请求 代理(proxy),这时,把Invocationhandler 以参数的形式 传递给 代理(proxy)

2,客户端 在调用  代理(proxy)的其他方法,其他方法,也调用 (InvocationHandler)。

3,而 InvocationHandler   就执行,invoke方法,而这时 invoke 就可以调用目标程序,来完成 业务逻辑实现。

4,在 invocationHandler里面 有个 log方法,这里是作为一个对象,在这个对象里面  有实现的方法,来完成一些业务,正好是 spring的面向切面编程。

[java]  view plain copy print ?
  1. package com.itm.itcast;  
  2.   
  3. import java.lang.reflect.Constructor;  
  4. import java.lang.reflect.InvocationHandler;  
  5. import java.lang.reflect.Method;  
  6. import java.lang.reflect.Proxy;  
  7. import java.util.ArrayList;  
  8. import java.util.Collection;  
  9.   
  10. public class ProxyTest{  
  11.   
  12.     /** 
  13.      * @param args 
  14.      * @throws Exception  
  15.      * @throws SecurityException  
  16.      */  
  17.     public static void main(String[] args) throws SecurityException, Exception {  
  18.           
  19.         Class clazzProxy1 = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);  
  20.         System.out.println(clazzProxy1.getName());  
  21.           
  22.           
  23.         System.out.println("  以下  :创建动态类的实例对象及调用其方法 ");  
  24.           
  25.         Constructor constructor = clazzProxy1.getConstructor(InvocationHandler.class);  
  26.           
  27.           
  28.         class MyInvocationHandler1 implements InvocationHandler{  
  29.   
  30.             @Override  
  31.             public Object invoke(Object proxy, Method method, Object[] args)  
  32.                     throws Throwable {  
  33.                 // TODO Auto-generated method stub  
  34.                 return null;  
  35.             }  
  36.               
  37.         }  
  38.           
  39.         Collection proxy1 = (Collection) constructor.newInstance(new MyInvocationHandler1());  
  40.         System.out.println(proxy1);  
  41.         // 下面两行代码在执行的时候   都会找 invoke的,而在invoke的方法里 返回的是 null,所以会报错。,。。  
  42.         proxy1.clear();  
  43.         proxy1.size();  
  44.         System.out.println("^^^^^^^^^^^");  
  45.           
  46.           
  47.         Collection proxy2 = (Collection)constructor.newInstance(new InvocationHandler(){  
  48.   
  49.             @Override  
  50.             public Object invoke(Object proxy, Method method, Object[] args)  
  51.                     throws Throwable {  
  52.                   
  53.                 return null;  
  54.             }  
  55.               
  56.         });  
  57.           
  58.         Collection proxy3 = (Collection)Proxy.newProxyInstance(  
  59.                 Collection.class.getClassLoader(),   
  60.                 /*interfaces*/   
  61.                 new Class[]{Collection.class},  
  62.                 /*h*/  
  63.                 new InvocationHandler(){  
  64.                     // 若放到这里:size() 就会打印 三。  
  65.                     /*(1)*/ArrayList target = new ArrayList();  
  66.                     @Override  
  67.                     public Object invoke(Object proxy, Method method, Object[] args)  
  68.                             throws Throwable {  
  69.                           
  70.                         /*(2)*/ArrayList target = new ArrayList();  
  71.                         long startTime = System.currentTimeMillis();  
  72.                         Object retVal = method.invoke(target, args);  
  73.                         long endTime = System.currentTimeMillis();  
  74.                         System.out.println(method.getName() + " :runing time of " + (endTime - startTime));  
  75.                         return  retVal;  
  76.                     }  
  77.               
  78.                 }  
  79.                 );  
  80.         // 每调用 一次 add方法  就会 找一次:InvocationHandler() 的  invoke 的方法。  
  81.         proxy3.add("zxx");  
  82.         proxy3.add("lhm");  
  83.         proxy3.add("kkkk");  
  84.         System.out.println(proxy3.size());  
  85.     }  
  86.   
  87. }  

以下方式 利用 动态代理:

[java]  view plain copy print ?
  1. package com.itm.proxy;  
  2.   
  3.   
  4.   
  5. import java.lang.reflect.InvocationHandler;  
  6. import java.lang.reflect.Method;  
  7. import java.lang.reflect.Proxy;  
  8. import java.util.ArrayList;  
  9. import java.util.Collection;  
  10.   
  11. public class ProxyTest {  
  12.   
  13.     /** 
  14.      * @param args 
  15.      */  
  16.     public static void main(String[] args) throws Exception{  
  17.         final ArrayList target = new ArrayList();             
  18.         Collection proxy3 = (Collection)getProxy(target,new MyAdvice());  
  19.         proxy3.add("aaa");  
  20.         proxy3.add("bbb");  
  21.         proxy3.add("ccc");  
  22.         System.out.println(proxy3.size());  
  23.         System.out.println(proxy3.getClass().getName());  
  24.     }  
  25.   
  26.     private static Object getProxy(final Object target,final Advice advice) {  
  27.         Object proxy3 = Proxy.newProxyInstance(  
  28.                 target.getClass().getClassLoader(),  
  29.                 target.getClass().getInterfaces(),  
  30.                 new InvocationHandler(){  
  31.                   
  32.                     public Object invoke(Object proxy, Method method, Object[] args)  
  33.                             throws Throwable {  
  34.                         advice.beforeMethod(method);  
  35.                         Object retVal = method.invoke(target, args);  
  36.                         advice.afterMethod(method);  
  37.                         return retVal;                        
  38.                           
  39.                     }  
  40.                 }  
  41.                 );  
  42.         return proxy3;  
  43.     }  
  44.   
  45. }  

接口:

[java]  view plain copy print ?
  1. import java.lang.reflect.Method;  
  2.   
  3. public interface Advice {  
  4.     void beforeMethod(Method method);  
  5.     void afterMethod(Method method);  
  6. }  

实现接口以及相应的业务逻辑:

[java]  view plain copy print ?
  1. import java.lang.reflect.Method;  
  2.   
  3. public class MyAdvice implements Advice {  
  4.     long beginTime = 0;  
  5.     public void afterMethod(Method method) {  
  6.         // TODO Auto-generated method stub  
  7.         System.out.println("方法后");        
  8.         long endTime = System.currentTimeMillis();  
  9.         System.out.println(method.getName() + " running time of " + (endTime - beginTime));  
  10.   
  11.     }  
  12.   
  13.     public void beforeMethod(Method method) {  
  14.         // TODO Auto-generated method stub  
  15.         System.out.println("方法前");  
  16.         beginTime = System.currentTimeMillis();  
  17.     }  
  18.   
  19. }  

运行结果:


方法前
方法后
add running time of 0
方法前
方法后
add running time of 0
方法前
方法后
add running time of 0
方法前
方法后
size running time of 0
3
$Proxy0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值