栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

详解java JDK 动态代理类分析(java.lang.reflect.Proxy)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解java JDK 动态代理类分析(java.lang.reflect.Proxy)

详解java JDK 动态代理类分析(java.lang.reflect.Proxy)

 
public class ProxyStudy { 
   
  @SuppressWarnings("unchecked") 
  public static void main(String[] args) throws Exception { 
    // 动态代理类:通用指定类加载器,和接口产生一类 
    // getProxyClass()返回代理类的 java.lang.Class 对象,并向其提供类加载器和接口数组。 
    Class clazzProxy = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class); 
    System.out.println("动态产生的类名为:" + clazzProxy.getName()); 
    System.out.println("----------获取动态产生的类的构造方法---------"); 
    Constructor[] constructors = clazzProxy.getConstructors(); 
    int i = 1; 
    for (Constructor constructor : constructors) { 
      System.out.println("第" + (i++) + "个构造方法名:" + constructor.getName()); 
      Class[] parameterClazz = constructor.getParameterTypes(); 
      System.out.println("第" + (i++) + "个构造方法参数:" + Arrays.asList(parameterClazz)); 
    } 
    System.out.println("----------获取动态产生的类的普通方法---------"); 
    Method[] methods = clazzProxy.getDeclaredMethods(); 
    for (int j = 0; j < methods.length; j++) { 
      Method method = methods[j]; 
      System.out.println("第" + (j + 1) + "个普通方法名:" + method.getName()); 
      Class[] parameterClazz = method.getParameterTypes(); 
      System.out.println("第" + (j + 1) + "个普通方法参数:" + Arrays.asList(parameterClazz)); 
    } 
    System.out.println("---------获取动态代理对象的构造方法---------"); 
    // 动态代理产生的对象的构造方法需要一个实现java.lang.reflect.InvocationHandler接口的对象,故不能通过 
    // clazzProxy.newInstance();产生一个对象,可以根据构造方法产生一个对象 
    // InvocationHandler 是代理实例的调用处理程序 实现的接口。 
    Constructor constructor = clazzProxy.getConstructor(InvocationHandler.class); 
 
    // 代理产生的对象 
    Collection proxyBuildCollection = (Collection) constructor 
 .newInstance(new InvocationHandler() { 
   // 为什么这里选择ArrayList作为目标对象? 
   // 因为这里的constructor是clazzProxy这个动态类的构造方法,clazzProxy是通过Proxy.getProxyClass()方法产生的, 
   // 该方法有两个参数,一个是指定类加载器,一个是指定代理要实现的接口,这个接口我上面指定了Collection 
   // 而ArrayList实现了Collection接口,固可以为该动态类的目标对象 
   ArrayList target = new ArrayList();// 动态类的目标对象 
 
   public Object invoke(Object proxy, Method method, 
Object[] args) throws Throwable { 
     System.out.println("执行目标" + method.getName() + "方法之前:" 
  + System.currentTimeMillis()); 
     Object result = method.invoke(target, args);// 其实代理对象的方法调用还是目标对象的方法 
     System.out.println("执行目标" + method.getName() + "方法之后:" 
  + System.currentTimeMillis()); 
     return result; 
   } 
 
 }); 
    proxyBuildCollection.clear(); 
    proxyBuildCollection.add("abc"); 
    proxyBuildCollection.add("dbc"); 
    System.out.println(proxyBuildCollection.size()); 
    System.out.println(proxyBuildCollection.getClass().getName()); 
     
     
    System.out.println("-------------------下面的写法更简便--------------------"); 
     
    // proxyBuildColl是对ArrayList进行代理 
    Collection proxyBuildCollection2 = (Collection) Proxy.newProxyInstance( 
 Collection.class.getClassLoader(),// 指定类加载器 
 new Class[] { Collection.class },// 指定目标对象实现的接口 
 // 指定handler 
 new InvocationHandler() { 
   ArrayList target = new ArrayList(); 
 
   public Object invoke(Object proxy, Method method, 
Object[] args) throws Throwable { 
     System.out.println(method.getName() + "执行之前..."); 
     if (null != args) { 
System.out.println("方法的参数:" + Arrays.asList(args)); 
     } else { 
System.out.println("方法的参数:" + null); 
     } 
     Object result = method.invoke(target, args); 
     System.out.println(method.getName() + "执行之后..."); 
     return result; 
   } 
 }); 
    proxyBuildCollection2.add("abc"); 
    proxyBuildCollection2.size(); 
    proxyBuildCollection2.clear(); 
    proxyBuildCollection2.getClass().getName(); 
     
    System.out.println("-------------------对JDK动态代理的重构--------------------"); 
    Set proxySet = (Set) buildProxy(new HashSet(), new MyAdvice()); 
    proxySet.add("abc"); 
    proxySet.size(); 
  } 
   
  public static Object buildProxy(final Object target,final AdviceInter advice) { 
    Object proxyObject = Proxy.newProxyInstance( 
 target.getClass().getClassLoader(),// 指定类加载器 
 target.getClass().getInterfaces(), // 指定目标对象实现的接口 
 // handler 
 new InvocationHandler() { 
    
   public Object invoke(Object proxy, Method method, 
Object[] args) throws Throwable { 
     advice.beforeMethod(target, method, args); 
     Object result = method.invoke(target, args); 
     advice.afterMethod(target, method, args); 
     return result; 
   } 
 }); 
    return proxyObject; 
  } 
   
} 
 
 
public class MyAdvice implements AdviceInter { 
 
  public void afterMethod(Object target, Method method, Object[] args) { 
    System.out.println("目标对象为:" + target.getClass().getName()); 
    System.out.println(method.getName() + "执行完毕!"); 
  } 
 
  public void beforeMethod(Object target, Method method, Object[] args) { 
    System.out.println(method.getName() + "开始执行"); 
    if (null != args) { 
      System.out.println("参数为:" + Arrays.asList(args)); 
    } else { 
      System.out.println("参数为:" + null); 
    } 
  } 
} 
 
public interface AdviceInter { 
   
  public void beforeMethod(Object target, Method method, Object[] args); 
 
   
  public void afterMethod(Object target, Method method, Object[] args); 
} 


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/145754.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号