dingfirst

          On the Road

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

          先看一下Proxy的兩種用法:

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


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

          ????
          }
          ???調(diào)用處理類,這也是aop的一種實(shí)現(xiàn)方式,呵呵。
          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);
          ????}

          }
          ???兩種實(shí)現(xiàn)方式:
          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)
          ??????????返回指定代理實(shí)例的調(diào)用處理程序。
          static?Class<?>getProxyClass(ClassLoader?loader, Class<?>...?interfaces)
          ??????????返回代理類的 java.lang.Class 對象,并向其提供類加載器和接口數(shù)組。
          static?booleanisProxyClass(Class<?>?cl)
          ??????????當(dāng)且僅當(dāng)指定的類通過 getProxyClass 方法或 newProxyInstance 方法動態(tài)生成為代理類時,返回 true。
          static?ObjectnewProxyInstance(ClassLoader?loader, Class<?>[]?interfaces, InvocationHandler?h)
          ??????????返回一個指定接口的代理類實(shí)例,該接口可以將方法調(diào)用指派到指定的調(diào)用處理程序。

          沒時間了,先這樣吧。


          ?

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

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 改则县| 平利县| 淳化县| 墨竹工卡县| 崇信县| 杭锦后旗| 绩溪县| 泗洪县| 盐城市| 江都市| 甘泉县| 肇庆市| 双鸭山市| 白沙| 独山县| 靖安县| 策勒县| 泰宁县| 凤庆县| 易门县| 汪清县| 吉首市| 迭部县| 平谷区| 民权县| 珠海市| 庄浪县| 马关县| 峨山| 竹北市| 泾源县| 乌恰县| 酒泉市| 金沙县| 紫金县| 杭锦后旗| 天祝| 辉县市| 德清县| 台中县| 高青县|