動(dòng)態(tài)代理,作為實(shí)現(xiàn)AOP的方式之一,已經(jīng)得到廣泛的應(yīng)用.本人看了很多書(shū)關(guān)于動(dòng)態(tài)代理的介紹,基本就是不知所云. 所以最終自己做了一個(gè)例子,才感到有點(diǎn)明白,下面是我的代碼

          (注: 說(shuō)明同一個(gè)包里面的類(lèi)的類(lèi)加載器(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 {???? // ?被代理的類(lèi)?

          ???????
          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();????????? // 先生成一個(gè)"被代理類(lèi)"的對(duì)象?

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

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

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

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

          ??????????????System.out.println(
          " After " );?????? // 方法調(diào)用之后輸出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);
          ??//被代理接口的類(lèi)加載器,被加載的類(lèi),和。。。。
          ????????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) 評(píng)論(2)  編輯  收藏
          Comments

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 绿春县| 正定县| 阿尔山市| 嘉鱼县| 县级市| 凉城县| 固镇县| 辰溪县| 颍上县| 沛县| 泊头市| 红桥区| 清远市| 长子县| 华坪县| 高邮市| 阳山县| 大洼县| 宝丰县| 攀枝花市| 将乐县| 长寿区| 大埔区| 图们市| 泾源县| 盐山县| 望奎县| 淮北市| 南城县| 巢湖市| 保靖县| 鄢陵县| 安义县| 当阳市| 福州市| 涿州市| 浪卡子县| 大英县| 青岛市| 海林市| 策勒县|