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 閱讀(273) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 南川市| 洮南市| 河间市| 扎囊县| 积石山| 夏津县| 邯郸县| 海兴县| 抚顺县| 伊川县| 卓尼县| 八宿县| 日喀则市| 察隅县| 东源县| 罗定市| 泰宁县| 抚远县| 香港 | 合阳县| 四会市| 山东省| 奉化市| 西乌| 阳春市| 桦甸市| 呼伦贝尔市| 建昌县| 贺州市| 邮箱| 辉南县| 韶山市| 于田县| 怀仁县| 吉安市| 兴安县| 梁河县| 炉霍县| 长寿区| 德格县| 儋州市|