dingfirst

          On the Road

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            8 隨筆 :: 2 文章 :: 3 評論 :: 0 Trackbacks

          先看一下Proxy的兩種用法:

          public ? interface ?Foo? {
          ????
          int ?doSomthing();
          }
          ???一個基本的實現:
          public?class?FooImpl?implements?Foo?{
          ????
          public?FooImpl()?{
          ????}


          ????
          public?int?doSomthing()?{
          ????????System.out.println(
          "FooImpl?doSomthing");
          ????????
          return?10;
          ????}

          ????
          }
          ???調用處理類,這也是aop的一種實現方式,呵呵。
          public?class?MyInvocationHandler?implements?InvocationHandler{
          ????Object?proxyObject;
          ????
          ????
          public?MyInvocationHandler(Object?_proxyObject)?{
          ????????
          this.proxyObject=_proxyObject;
          ????}


          ????
          public?Object?invoke(Object?proxy,?Method?method,?Object[]?args)?throws?Throwable?{
          ????????System.out.println(
          "in?proxy?instance");
          ????????
          return?method.invoke(proxyObject,args);
          ????}

          }
          ???兩種實現方式:
          public?class?ProxyUse?{
          ????
          public?ProxyUse()?{
          ????}


          ????
          public?void?useProxy1()?throws?SecurityException,?NoSuchMethodException,?InvocationTargetException,
          ????????????IllegalArgumentException,?IllegalAccessException,?InstantiationException?
          {
          ????????FooImpl?obj?
          =?new?FooImpl();
          ????????InvocationHandler?handler?
          =?new?MyInvocationHandler(obj);
          ????????Class?proxyClass?
          =?Proxy.getProxyClass(
          ????????????????Foo.
          class.getClassLoader(),?new?Class[]?{Foo.class});
          ????????Foo?f?
          =?(Foo)?proxyClass.
          ????????????????getConstructor(
          new?Class[]?{InvocationHandler.class}).
          ????????????????newInstance(
          new?Object[]?{handler});
          ????????System.out.println(f.doSomthing());
          ????}

          ????
          ????
          public?void?useProxy2()?throws?SecurityException,?NoSuchMethodException,?InvocationTargetException,
          ????????????IllegalArgumentException,?IllegalAccessException,?InstantiationException?
          {
          ????????FooImpl?obj?
          =?new?FooImpl();
          ????????InvocationHandler?handler?
          =?new?MyInvocationHandler(obj);
          ????????Foo?f?
          =?(Foo)?Proxy.newProxyInstance(
          ????????????Foo.
          class.getClassLoader(),
          ????????????
          new?Class[]?{?Foo.class?},
          ????????????handler
          ????????????);
          ????????System.out.println(f.doSomthing());
          ????}

          ????
          ????
          public?static?void?main(String?[]?args){
          ????????ProxyUse?use
          =new?ProxyUse();
          ????????
          try{
          ????????????use.useProxy1();
          ????????????use.useProxy2();
          ????????}
          catch(Exception?ex){
          ????????????ex.printStackTrace();
          ????????}

          ????}


          }

          看一下java api doc

          static?InvocationHandlergetInvocationHandler(Object?proxy)
          ??????????返回指定代理實例的調用處理程序。
          static?Class<?>getProxyClass(ClassLoader?loader, Class<?>...?interfaces)
          ??????????返回代理類的 java.lang.Class 對象,并向其提供類加載器和接口數組。
          static?booleanisProxyClass(Class<?>?cl)
          ??????????當且僅當指定的類通過 getProxyClass 方法或 newProxyInstance 方法動態生成為代理類時,返回 true。
          static?ObjectnewProxyInstance(ClassLoader?loader, Class<?>[]?interfaces, InvocationHandler?h)
          ??????????返回一個指定接口的代理類實例,該接口可以將方法調用指派到指定的調用處理程序。

          沒時間了,先這樣吧。


          ?

          posted on 2006-07-26 17:48 dingfirst 閱讀(267) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 永寿县| 沧源| 商丘市| 伊川县| 桓台县| 金塔县| 上饶市| 张掖市| 兴化市| 高安市| 鹤庆县| 杂多县| 辽阳县| 都安| 扎鲁特旗| 惠来县| 玛纳斯县| 宜宾县| 赤水市| 济宁市| 鸡西市| 永宁县| 灌阳县| 高碑店市| 堆龙德庆县| 隆子县| 婺源县| 亚东县| 普洱| 同江市| 九龙县| 永昌县| 酉阳| 呼图壁县| 漾濞| 兴隆县| 大同市| 高平市| 阜城县| 太原市| 河北区|