動態代理,作為實現AOP的方式之一,已經得到廣泛的應用.本人看了很多書關于動態代理的介紹,基本就是不知所云. 所以最終自己做了一個例子,才感到有點明白,下面是我的代碼

          (注: 說明同一個包里面的類的類加載器(ClassLoader)是一樣的)

          package ?pear;?

          ??

          // import?org.springframework.aop.Advisor;?

          // import?org.springframework.aop.BeforeAdvice;?

          import ?java.lang.reflect. * ;?

          ??

          interface ?SayInterface {?????????? // ?被代理接口?

          ???????
          public ? void ?say();?

          ???????
          public ? void ?saytwo();?

          }
          ?

          class ?Say? implements ?SayInterface {???? // ?被代理的類?

          ???????
          public ? void ?say() {?

          ??????????????System.out.println(
          " ?in?say? " );?

          ???????}
          ?

          ???????
          public ? void ?saytwo() {?

          ??????????????System.out.println(
          " in?say?2 " );?

          ???????}
          ?

          }
          ?

          ??

          class ?SayHandler? implements ?InvocationHandler {????? // ?

          ???????
          private ?Say?mysay? = ? new ?Say();????????? // 先生成一個"被代理類"的對象?

          ???????
          public ?Object?invoke(Object?proxy,Method?method,Object[]?args) throws ?Exception {?

          ??????????????System.out.println(
          " Before " );???? // 方法調用之前輸出Before?

          ???????????method.invoke(mysay,args);?????????
          // 原來方法在這里才真正被調用?

          ??????????????
          // method.invoke(saytwo,args);?

          ??????????????System.out.println(
          " After " );?????? // 方法調用之后輸出After?

          ??????????????
          return ? null ;?

          ???????}
          ?

          }
          ?

          ??

          public ? class ?Main? {?

          ?
          /**
          ??*?
          @param ?args
          ??
          */

          ?
          public ? static ? void ?main(String[]?args)? {
          ??
          // ?TODO?Auto-generated?method?stub
          ??SayHandler?handler? = ? new ?SayHandler();
          ??SayInterface?si?
          = ?(SayInterface)Proxy.newProxyInstance(
          ????SayInterface.
          class .getClassLoader(), new ?Class[] {SayInterface. class } ,handler);
          /** ??SayInterface?si?=?(SayInterface)Proxy.newProxyInstance(
          //???????handler.getClass().getClassLoader(),handler.say.getClass().getInterfaces(),handler);
          ??//被代理接口的類加載器,被加載的類,和。。。。
          ????????SayInterface?si?=?(SayInterface)Proxy.newProxyInstance(handler.getClass().getClassLoader(),);???
          */
          ?
          ???si.say();
          ??si.saytwo();
          ??System.out.println(
          " the?end. " );
          ?}
          ?

          }


          posted on 2006-06-10 20:46 pear 閱讀(1181) 評論(2)  編輯  收藏
          Comments

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


          網站導航:
           
           
          主站蜘蛛池模板: 兰西县| 鄂伦春自治旗| 宁武县| 东丽区| 安宁市| 长海县| 淅川县| 民乐县| 温宿县| 布拖县| 张家界市| 武宣县| 抚州市| 陵川县| 邵东县| 福清市| 淮安市| 革吉县| 余姚市| 南充市| 房产| 伊川县| 庆城县| 乌兰浩特市| 汕头市| 宁武县| 新巴尔虎左旗| 屏边| 岳西县| 寿光市| 虞城县| 郁南县| 石河子市| 尚义县| 翁牛特旗| 秦皇岛市| 平顺县| 台北县| 沐川县| 铜鼓县| 松原市|