最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解java JDK 動態(tài)代理類分析(java.lang.reflect.Proxy)

 更新時間:2017年06月08日 17:26:57   作者:Alex_zhuang  
這篇文章主要介紹了詳解java JDK 動態(tài)代理類分析(java.lang.reflect.Proxy)的相關(guān)資料,需要的朋友可以參考下

詳解java JDK 動態(tài)代理類分析(java.lang.reflect.Proxy)

/** 
 * JDK 動態(tài)代理類分析(java.lang.reflect.Proxy使用) 
 * 
 * @author 張明學(xué) 
 * 
 */ 
public class ProxyStudy { 
   
  @SuppressWarnings("unchecked") 
  public static void main(String[] args) throws Exception { 
    // 動態(tài)代理類:通用指定類加載器,和接口產(chǎn)生一類 
    // getProxyClass()返回代理類的 java.lang.Class 對象,并向其提供類加載器和接口數(shù)組。 
    Class clazzProxy = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class); 
    System.out.println("動態(tài)產(chǎn)生的類名為:" + clazzProxy.getName()); 
    System.out.println("----------獲取動態(tài)產(chǎn)生的類的構(gòu)造方法---------"); 
    Constructor[] constructors = clazzProxy.getConstructors(); 
    int i = 1; 
    for (Constructor constructor : constructors) { 
      System.out.println("第" + (i++) + "個構(gòu)造方法名:" + constructor.getName()); 
      Class[] parameterClazz = constructor.getParameterTypes(); 
      System.out.println("第" + (i++) + "個構(gòu)造方法參數(shù):" + Arrays.asList(parameterClazz)); 
    } 
    System.out.println("----------獲取動態(tài)產(chǎn)生的類的普通方法---------"); 
    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) + "個普通方法參數(shù):" + Arrays.asList(parameterClazz)); 
    } 
    System.out.println("---------獲取動態(tài)代理對象的構(gòu)造方法---------"); 
    // 動態(tài)代理產(chǎn)生的對象的構(gòu)造方法需要一個實(shí)現(xiàn)java.lang.reflect.InvocationHandler接口的對象,故不能通過 
    // clazzProxy.newInstance();產(chǎn)生一個對象,可以根據(jù)構(gòu)造方法產(chǎn)生一個對象 
    // InvocationHandler 是代理實(shí)例的調(diào)用處理程序 實(shí)現(xiàn)的接口。 
    Constructor constructor = clazzProxy.getConstructor(InvocationHandler.class); 
 
    // 代理產(chǎn)生的對象 
    Collection proxyBuildCollection = (Collection) constructor 
        .newInstance(new InvocationHandler() { 
          // 為什么這里選擇ArrayList作為目標(biāo)對象? 
          // 因?yàn)檫@里的constructor是clazzProxy這個動態(tài)類的構(gòu)造方法,clazzProxy是通過Proxy.getProxyClass()方法產(chǎn)生的, 
          // 該方法有兩個參數(shù),一個是指定類加載器,一個是指定代理要實(shí)現(xiàn)的接口,這個接口我上面指定了Collection 
          // 而ArrayList實(shí)現(xiàn)了Collection接口,固可以為該動態(tài)類的目標(biāo)對象 
          ArrayList target = new ArrayList();// 動態(tài)類的目標(biāo)對象 
 
          public Object invoke(Object proxy, Method method, 
              Object[] args) throws Throwable { 
            System.out.println("執(zhí)行目標(biāo)" + method.getName() + "方法之前:" 
                + System.currentTimeMillis()); 
            Object result = method.invoke(target, args);// 其實(shí)代理對象的方法調(diào)用還是目標(biāo)對象的方法 
            System.out.println("執(zhí)行目標(biāo)" + 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()); 
     
    /** 
     * 動態(tài)代理:總結(jié)如下: 
     * 1,通過Proxy.getProxyClass(classLoader,interface)方法產(chǎn)生一個動態(tài)類的class字節(jié)碼(clazz) 
     *  該getProxyClass()方法有兩個參數(shù):一個是指定該動態(tài)類的類加載器,一個是該動態(tài)類的要實(shí)現(xiàn)的接口(從這里可以看現(xiàn)JDK的動態(tài)代理必須要實(shí)現(xiàn)一個接口) 
     *   
     * 2,通過第一步的獲取的clazz對象可以獲取它的構(gòu)造方法constructor,那么就可以通用constructor的newInstance()方法構(gòu)造出一個動態(tài)實(shí)體對象 
     *  但constructor的newInstance()方法需要指定一個實(shí)現(xiàn)了InvocationHandler接口的類handler,在該類中需要一個目標(biāo)對象A和實(shí)現(xiàn)invoke方法 
     *  目標(biāo)對象A要求能對第一步中的接口的實(shí)現(xiàn),因?yàn)樵趇nvoke方法中將會去調(diào)用A中的方法并返回結(jié)果。 
     *  過程如下:調(diào)用動態(tài)代理對象ProxyObject的x方法 ————> 進(jìn)入構(gòu)造方法傳進(jìn)的handler的invoke方法 ————> invoke方法調(diào)用handler中的target對象 
     *      的x方法(所以要求target必須要實(shí)現(xiàn)構(gòu)造動態(tài)代理類時指定的接口)并返回它的返回值。(其實(shí)如果我們代理P類,那么target就可以選中P類,只是要求P必需實(shí)現(xiàn)一個接口) 
     *   
     *  那么上述中x方法有哪些呢?除了從Object繼承過來的方法中除toString,hashCode,equals外的方法不交給handler外,其它的方法全部交給handler處理 
     *  如上面proxyBuildCollection.getClass().getName()就沒有調(diào)用handler的getClass方法,而是調(diào)用自己的 
     *   
     * 3,在handler的invoke方法中return method.invoke(target,args)就是將方法交給target去完成。那么在這個方法執(zhí)行之前,之后,異常時我們都可以做一些操作, 
     *  并且可以在執(zhí)行之前檢查方法的參數(shù)args,執(zhí)行之后檢查方法的結(jié)果 
     */ 
    System.out.println("-------------------下面的寫法更簡便--------------------"); 
     
    // proxyBuildColl是對ArrayList進(jìn)行代理 
    Collection proxyBuildCollection2 = (Collection) Proxy.newProxyInstance( 
        Collection.class.getClassLoader(),// 指定類加載器 
        new Class[] { Collection.class },// 指定目標(biāo)對象實(shí)現(xiàn)的接口 
        // 指定handler 
        new InvocationHandler() { 
          ArrayList target = new ArrayList(); 
 
          public Object invoke(Object proxy, Method method, 
              Object[] args) throws Throwable { 
            System.out.println(method.getName() + "執(zhí)行之前..."); 
            if (null != args) { 
              System.out.println("方法的參數(shù):" + Arrays.asList(args)); 
            } else { 
              System.out.println("方法的參數(shù):" + null); 
            } 
            Object result = method.invoke(target, args); 
            System.out.println(method.getName() + "執(zhí)行之后..."); 
            return result; 
          } 
        }); 
    proxyBuildCollection2.add("abc"); 
    proxyBuildCollection2.size(); 
    proxyBuildCollection2.clear(); 
    proxyBuildCollection2.getClass().getName(); 
     
    System.out.println("-------------------對JDK動態(tài)代理的重構(gòu)--------------------"); 
    Set proxySet = (Set) buildProxy(new HashSet(), new MyAdvice()); 
    proxySet.add("abc"); 
    proxySet.size(); 
  } 
  /** 
   * 構(gòu)造一個目標(biāo)對象的代理對象 
   * 
   * @param target 
   *      目標(biāo)對象(需要實(shí)現(xiàn)某個接口) 
   * @return 
   */ 
  public static Object buildProxy(final Object target,final AdviceInter advice) { 
    Object proxyObject = Proxy.newProxyInstance( 
        target.getClass().getClassLoader(),// 指定類加載器 
        target.getClass().getInterfaces(), // 指定目標(biāo)對象實(shí)現(xiàn)的接口 
        // 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; 
  } 
   
} 
 
/** 
 * 代理中執(zhí)行目標(biāo)方法之前之后的操作的一個實(shí)例 
 * 
 * @author 張明學(xué) 
 * 
 */ 
public class MyAdvice implements AdviceInter { 
 
  public void afterMethod(Object target, Method method, Object[] args) { 
    System.out.println("目標(biāo)對象為:" + target.getClass().getName()); 
    System.out.println(method.getName() + "執(zhí)行完畢!"); 
  } 
 
  public void beforeMethod(Object target, Method method, Object[] args) { 
    System.out.println(method.getName() + "開始執(zhí)行"); 
    if (null != args) { 
      System.out.println("參數(shù)為:" + Arrays.asList(args)); 
    } else { 
      System.out.println("參數(shù)為:" + null); 
    } 
  } 
} 
/** 
 * 代理中執(zhí)行目標(biāo)方法之前之后的操作 
 * 
 * @author 張明學(xué) 
 * 
 */ 
public interface AdviceInter { 
  /** 
   * 目標(biāo)方法執(zhí)行之前 
   * 
   */ 
  public void beforeMethod(Object target, Method method, Object[] args); 
 
  /** 
   * 目標(biāo)方法執(zhí)行之后 
   * 
   * @param target 
   *      目標(biāo)對象 
   * @param method 
   *      方法 
   * @param args 
   *      參數(shù) 
   */ 
  public void afterMethod(Object target, Method method, Object[] args); 
} 


 

相關(guān)文章

  • spring cloud oauth2 實(shí)現(xiàn)用戶認(rèn)證登錄的示例代碼

    spring cloud oauth2 實(shí)現(xiàn)用戶認(rèn)證登錄的示例代碼

    這篇文章主要介紹了spring cloud oauth2 實(shí)現(xiàn)用戶認(rèn)證登錄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Springboot使用切面功能詳解

    Springboot使用切面功能詳解

    Spring?Boot?是一個基于Spring框架的項(xiàng)目,它簡化了基于Spring的應(yīng)用開發(fā),這篇文章主要介紹了?Spring?Boot?中的切面功能,需要的可以了解下
    2025-01-01
  • java實(shí)戰(zhàn)之桌球小游戲

    java實(shí)戰(zhàn)之桌球小游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)戰(zhàn)之桌球小游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • .NET程序員如何入門Spring Boot詳解

    .NET程序員如何入門Spring Boot詳解

    這篇文章主要介紹了.NET程序員如何快入門Spring Boot,微軟給VS Code提供了編寫Java的插件,你可以在VS Code上愉快地寫Java。,需要的朋友可以參考下
    2019-06-06
  • 淺談Java中Properties類的詳細(xì)使用

    淺談Java中Properties類的詳細(xì)使用

    properties類繼承自hashtable,通常和io流結(jié)合使用。它最突出的特點(diǎn)是將key/value作為配置屬性寫入到配置文件中以實(shí)現(xiàn)配置持久化,或從配置文件中讀取這些屬性。它的這些配置文件的規(guī)范后綴名為".properties"。表示了一個持久的屬性集
    2021-06-06
  • Java微信公眾平臺開發(fā)(5) 文本及圖文消息回復(fù)的實(shí)現(xiàn)

    Java微信公眾平臺開發(fā)(5) 文本及圖文消息回復(fù)的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺開發(fā)第五步,回文本及圖文消息回復(fù)的實(shí)現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Java之map的常見用法講解與五種循環(huán)遍歷實(shí)例代碼理解

    Java之map的常見用法講解與五種循環(huán)遍歷實(shí)例代碼理解

    map是一組鍵值對的組合,通俗理解類似一種特殊的數(shù)組,a[key]=val,只不過數(shù)組元素的下標(biāo)是任意一種類型,而且數(shù)組的元素的值也是任意一種類型。有點(diǎn)類似python中的字典。通過"鍵"來取值,類似生活中的字典,已知索引,來查看對應(yīng)的信息
    2021-09-09
  • 快速學(xué)習(xí)JavaWeb中監(jiān)聽器(Listener)的使用方法

    快速學(xué)習(xí)JavaWeb中監(jiān)聽器(Listener)的使用方法

    這篇文章主要幫助大家快速學(xué)習(xí)JavaWeb中監(jiān)聽器(Listener)的使用方法,感興趣的小伙伴們可以參考一下
    2016-09-09
  • java-collection中的null,isEmpty用法

    java-collection中的null,isEmpty用法

    這篇文章主要介紹了java-collection中的null,isEmpty用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • java調(diào)用相互依賴的dll的處理方法

    java調(diào)用相互依賴的dll的處理方法

    大家好,本篇文章主要講的是java調(diào)用相互依賴的dll的處理方法,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01

最新評論

滨海县| 响水县| 绥宁县| 荆州市| 温宿县| 黑河市| 敦化市| 广丰县| 赤城县| 武胜县| 乐平市| 濉溪县| 永新县| 阿瓦提县| 合肥市| 集贤县| 松江区| 承德市| 黎平县| 平阴县| 林州市| 北宁市| 安达市| 辰溪县| 哈密市| 临夏县| 梧州市| 策勒县| 怀安县| 建瓯市| 肃北| 宜良县| 钟祥市| 固阳县| 通江县| 长阳| 河源市| 和顺县| 泸定县| 峨边| 大足县|