JDK 动态代理类分析(java.lang.reflect.Proxy使用)

本文详细解析了Java中JDK动态代理的实现原理与使用方式,包括如何利用Proxy类创建动态代理对象,以及如何通过InvocationHandler接口实现方法拦截。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java代码  
  1. /** 
  2.  * JDK 动态代理类分析(java.lang.reflect.Proxy使用) 
  3.  *  
  4.  * @author 张明学 
  5.  *  
  6.  */  
  7. public class ProxyStudy {  
  8.       
  9.     @SuppressWarnings("unchecked")  
  10.     public static void main(String[] args) throws Exception {  
  11.         // 动态代理类:通用指定类加载器,和接口产生一类  
  12.         // getProxyClass()返回代理类的 java.lang.Class 对象,并向其提供类加载器和接口数组。  
  13.         Class clazzProxy = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);  
  14.         System.out.println("动态产生的类名为:" + clazzProxy.getName());  
  15.         System.out.println("----------获取动态产生的类的构造方法---------");  
  16.         Constructor[] constructors = clazzProxy.getConstructors();  
  17.         int i = 1;  
  18.         for (Constructor constructor : constructors) {  
  19.             System.out.println("第" + (i++) + "个构造方法名:" + constructor.getName());  
  20.             Class[] parameterClazz = constructor.getParameterTypes();  
  21.             System.out.println("第" + (i++) + "个构造方法参数:" + Arrays.asList(parameterClazz));  
  22.         }  
  23.         System.out.println("----------获取动态产生的类的普通方法---------");  
  24.         Method[] methods = clazzProxy.getDeclaredMethods();  
  25.         for (int j = 0; j < methods.length; j++) {  
  26.             Method method = methods[j];  
  27.             System.out.println("第" + (j + 1) + "个普通方法名:" + method.getName());  
  28.             Class[] parameterClazz = method.getParameterTypes();  
  29.             System.out.println("第" + (j + 1) + "个普通方法参数:" + Arrays.asList(parameterClazz));  
  30.         }  
  31.         System.out.println("---------获取动态代理对象的构造方法---------");  
  32.         // 动态代理产生的对象的构造方法需要一个实现java.lang.reflect.InvocationHandler接口的对象,故不能通过  
  33.         // clazzProxy.newInstance();产生一个对象,可以根据构造方法产生一个对象  
  34.         // InvocationHandler 是代理实例的调用处理程序 实现的接口。  
  35.         Constructor constructor = clazzProxy.getConstructor(InvocationHandler.class);  
  36.   
  37.         // 代理产生的对象  
  38.         Collection proxyBuildCollection = (Collection) constructor  
  39.                 .newInstance(new InvocationHandler() {  
  40.                     // 为什么这里选择ArrayList作为目标对象?  
  41.                     // 因为这里的constructor是clazzProxy这个动态类的构造方法,clazzProxy是通过Proxy.getProxyClass()方法产生的,  
  42.                     // 该方法有两个参数,一个是指定类加载器,一个是指定代理要实现的接口,这个接口我上面指定了Collection  
  43.                     // 而ArrayList实现了Collection接口,固可以为该动态类的目标对象  
  44.                     ArrayList target = new ArrayList();// 动态类的目标对象  
  45.   
  46.                     public Object invoke(Object proxy, Method method,  
  47.                             Object[] args) throws Throwable {  
  48.                         System.out.println("执行目标" + method.getName() + "方法之前:"  
  49.                                 + System.currentTimeMillis());  
  50.                         Object result = method.invoke(target, args);// 其实代理对象的方法调用还是目标对象的方法  
  51.                         System.out.println("执行目标" + method.getName() + "方法之后:"  
  52.                                 + System.currentTimeMillis());  
  53.                         return result;  
  54.                     }  
  55.   
  56.                 });  
  57.         proxyBuildCollection.clear();  
  58.         proxyBuildCollection.add("abc");  
  59.         proxyBuildCollection.add("dbc");  
  60.         System.out.println(proxyBuildCollection.size());  
  61.         System.out.println(proxyBuildCollection.getClass().getName());  
  62.           
  63.         /** 
  64.          * 动态代理:总结如下: 
  65.          * 1,通过Proxy.getProxyClass(classLoader,interface)方法产生一个动态类的class字节码(clazz) 
  66.          *    该getProxyClass()方法有两个参数:一个是指定该动态类的类加载器,一个是该动态类的要实现的接口(从这里可以看现JDK的动态代理必须要实现一个接口) 
  67.          *     
  68.          * 2,通过第一步的获取的clazz对象可以获取它的构造方法constructor,那么就可以通用constructor的newInstance()方法构造出一个动态实体对象 
  69.          *    但constructor的newInstance()方法需要指定一个实现了InvocationHandler接口的类handler,在该类中需要一个目标对象A和实现invoke方法 
  70.          *    目标对象A要求能对第一步中的接口的实现,因为在invoke方法中将会去调用A中的方法并返回结果。 
  71.          *    过程如下:调用动态代理对象ProxyObject的x方法 ————> 进入构造方法传进的handler的invoke方法 ————> invoke方法调用handler中的target对象 
  72.          *            的x方法(所以要求target必须要实现构造动态代理类时指定的接口)并返回它的返回值。(其实如果我们代理P类,那么target就可以选中P类,只是要求P必需实现一个接口) 
  73.          *     
  74.          *    那么上述中x方法有哪些呢?除了从Object继承过来的方法中除toString,hashCode,equals外的方法不交给handler外,其它的方法全部交给handler处理 
  75.          *    如上面proxyBuildCollection.getClass().getName()就没有调用handler的getClass方法,而是调用自己的 
  76.          *     
  77.          * 3,在handler的invoke方法中return method.invoke(target,args)就是将方法交给target去完成。那么在这个方法执行之前,之后,异常时我们都可以做一些操作, 
  78.          *    并且可以在执行之前检查方法的参数args,执行之后检查方法的结果 
  79.          */  
  80.         System.out.println("-------------------下面的写法更简便--------------------");  
  81.           
  82.         // proxyBuildColl是对ArrayList进行代理  
  83.         Collection proxyBuildCollection2 = (Collection) Proxy.newProxyInstance(  
  84.                 Collection.class.getClassLoader(),// 指定类加载器  
  85.                 new Class[] { Collection.class },// 指定目标对象实现的接口  
  86.                 // 指定handler  
  87.                 new InvocationHandler() {  
  88.                     ArrayList target = new ArrayList();  
  89.   
  90.                     public Object invoke(Object proxy, Method method,  
  91.                             Object[] args) throws Throwable {  
  92.                         System.out.println(method.getName() + "执行之前...");  
  93.                         if (null != args) {  
  94.                             System.out.println("方法的参数:" + Arrays.asList(args));  
  95.                         } else {  
  96.                             System.out.println("方法的参数:" + null);  
  97.                         }  
  98.                         Object result = method.invoke(target, args);  
  99.                         System.out.println(method.getName() + "执行之后...");  
  100.                         return result;  
  101.                     }  
  102.                 });  
  103.         proxyBuildCollection2.add("abc");  
  104.         proxyBuildCollection2.size();  
  105.         proxyBuildCollection2.clear();  
  106.         proxyBuildCollection2.getClass().getName();  
  107.           
  108.         System.out.println("-------------------对JDK动态代理的重构--------------------");  
  109.         Set proxySet = (Set) buildProxy(new HashSet(), new MyAdvice());  
  110.         proxySet.add("abc");  
  111.         proxySet.size();  
  112.     }  
  113.     /** 
  114.      * 构造一个目标对象的代理对象 
  115.      *  
  116.      * @param target 
  117.      *            目标对象(需要实现某个接口) 
  118.      * @return 
  119.      */  
  120.     public static Object buildProxy(final Object target,final AdviceInter advice) {  
  121.         Object proxyObject = Proxy.newProxyInstance(  
  122.                 target.getClass().getClassLoader(),// 指定类加载器  
  123.                 target.getClass().getInterfaces(), // 指定目标对象实现的接口  
  124.                 // handler  
  125.                 new InvocationHandler() {  
  126.                       
  127.                     public Object invoke(Object proxy, Method method,  
  128.                             Object[] args) throws Throwable {  
  129.                         advice.beforeMethod(target, method, args);  
  130.                         Object result = method.invoke(target, args);  
  131.                         advice.afterMethod(target, method, args);  
  132.                         return result;  
  133.                     }  
  134.                 });  
  135.         return proxyObject;  
  136.     }  
  137.       
  138. }  

 

Java代码  收藏代码
  1. /** 
  2.  * 代理中执行目标方法之前之后的操作的一个实例 
  3.  *  
  4.  * @author 张明学 
  5.  *  
  6.  */  
  7. public class MyAdvice implements AdviceInter {  
  8.   
  9.     public void afterMethod(Object target, Method method, Object[] args) {  
  10.         System.out.println("目标对象为:" + target.getClass().getName());  
  11.         System.out.println(method.getName() + "执行完毕!");  
  12.     }  
  13.   
  14.     public void beforeMethod(Object target, Method method, Object[] args) {  
  15.         System.out.println(method.getName() + "开始执行");  
  16.         if (null != args) {  
  17.             System.out.println("参数为:" + Arrays.asList(args));  
  18.         } else {  
  19.             System.out.println("参数为:" + null);  
  20.         }  
  21.     }  
  22. }  

 

Java代码  收藏代码
  1. /** 
  2.  * 代理中执行目标方法之前之后的操作 
  3.  *  
  4.  * @author 张明学 
  5.  *  
  6.  */  
  7. public interface AdviceInter {  
  8.     /** 
  9.      * 目标方法执行之前 
  10.      *  
  11.      */  
  12.     public void beforeMethod(Object target, Method method, Object[] args);  
  13.   
  14.     /** 
  15.      * 目标方法执行之后 
  16.      *  
  17.      * @param target 
  18.      *            目标对象 
  19.      * @param method 
  20.      *            方法 
  21.      * @param args 
  22.      *            参数 
  23.      */  
  24.     public void afterMethod(Object target, Method method, Object[] args);  

     本文转自http://zmx.iteye.com/blog/678416

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值