夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          AOP(面向方面編程)是一種新的編程技術(shù),它能夠?qū)⒛切┍静粦摷m纏在一起的任務分離開,從而為程序提供更好的封裝性和互操作性.
          AOP是通過代理機制實現(xiàn)的.
          代理是指為其他對象提供一種代理以控制對這個對象的訪問.
          代理分為三種:
              1.靜態(tài)代理,代理對象必須實現(xiàn)目標對象的接口,且一個接口只服務于一種類型的目標對象。
              2.JDK動態(tài)代理,代理對象必須實現(xiàn)java.lang.reflect.InvacationHandler接口,只能為接口創(chuàng)建代理實例。
              3.CGLib動態(tài)代理,使用非常底層的字節(jié)碼技術(shù),可以為任何對象創(chuàng)建代理.

          以下是目標對象接口和目標對象.
          目標對象接口如下:

          package org.test.spring.aop;

          public interface IHello
          {
              
          public void hello(String name);
          }
          目標對象如下:
          package org.test.spring.aop;

          public class HelloSpeaker implements IHello
          {
              
          public void hello(String name)
              {
                  System.out.print(
          "Hi," + name);
              }
          }


          一.靜態(tài)代理
          靜態(tài)代理比較容易理解,靜態(tài)代理其實很容易理解,靜態(tài)代理其實就是個裝飾器而已.
          讓代理對象也實現(xiàn)目標對像的接口,這樣做的目的是讓使用者在使用代理對象時感覺不到代理對象的存在.
          代理對象如下:

          package org.test.spring.aop;

          public class HelloSpeakerProxy implements IHello
          {
              
          private IHello helloSpeaker;
              
              
          public HelloSpeakerProxy(IHello helloSpeaker)
              {
                  
          this.helloSpeaker = helloSpeaker;
              }
              
              
          public void hello(String name)
              {
                  
          this.helloSpeaker.hello(name);
                  System.out.println(
          ", welcome to our pub.");
              }
              
              
          public static void main(String[] args)
              {
                  IHello helloSpeaker 
          = new HelloSpeaker();
                  IHello proxy 
          = new HelloSpeakerProxy(helloSpeaker);
                  proxy.hello(
          "Huy Vanpon");
              }
          }


          二.JDK動態(tài)代理

          所謂Dynamic Proxy是這樣一種class:它是在運行時生成的class,在生成它時你必須提供一組interface給它,然后該class就宣稱它實現(xiàn)了這些interface。你當然可以把該class的實例當作這些interface中的任何一個來用。當然啦,這個Dynamic Proxy其實就是一個Proxy,它不會替你作實質(zhì)性的工作,在生成它的實例時你必須提供一個handler,由它接管實際的工作。
          package org.test.spring.aop;

          import java.lang.reflect.InvocationHandler;
          import java.lang.reflect.Method;
          import java.lang.reflect.Proxy;

          public class HelloSpeakerProx implements InvocationHandler
          {
              
              
          private Object target;
              
              
          public Object invoke(Object proxy, Method method, Object[] parameters)
                      
          throws Throwable
              {
                  Object object 
          = null;
                  
          try
                  {
                      object 
          = method.invoke(target, parameters);
                      System.out.println(
          ", welcome to our pub.");
                  }
                  
          catch (Exception e)
                  {
                      e.printStackTrace();
                  }
                  
          return object;
              }
              
              
          /**
               * <p>
               * 這個方法用于得到代理對象
               * </p>
               * 
               * 
          @param target
               *            目標對象
               * 
          @return result(Object)代理對象
               
          */
              
          public Object getProxy(Object target)
              {
                  
          this.target = target;
                  
          /**
                   * newProxyInstance(ClassLoader loader, Class<?>[] interfaces,
                   * InvocationHandler invocationHandler) loader: 定義代理類的加加載器(目標類的加載器)
                   * interfaces: 代理類要實現(xiàn)的接口列表 invocationHandler: 指派方法調(diào)用的調(diào)用處理程序
                   
          */
                  
          return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                          target.getClass().getInterfaces(), 
          this);
              }
              
              
          public static void main(String[] args)
              {
                  IHello proxy 
          = (IHello) new HelloSpeakerProx()
                          .getProxy(
          new HelloSpeaker());
                  proxy.hello(
          "Huy Vanpon");
              }
          }


          三.CGLib動態(tài)代理
          使用jdk創(chuàng)建代理有一個限制,即它只能為接口創(chuàng)建代理實例.CGLib使用非常底層的字節(jié)碼技術(shù),可以為一個類創(chuàng)建子類,并在子類中采用方法攔截的攔截技術(shù),并順勢織入橫切邏輯.
          運行這個例子,一定要記得導入CGLib相應的包哦..
          package org.test.spring.aop;

          import java.lang.reflect.Method;

          import net.sf.cglib.proxy.Enhancer;
          import net.sf.cglib.proxy.MethodInterceptor;
          import net.sf.cglib.proxy.MethodProxy;

          public class HelloSpeakerPro implements MethodInterceptor
          {
              
          private Enhancer enhancer = new Enhancer();
              
              
          public Object getProxy(Class clazz)
              
          {
                  enhancer.setSuperclass(clazz);
                  enhancer.setCallback(
          this);
                  
          return enhancer.create();
              }

              
              
          public Object intercept(Object object, Method method, Object[] args,
                      MethodProxy proxy) 
          throws Throwable
              
          {
                  Object result 
          = null;
                  result 
          = proxy.invokeSuper(object, args);
                  System.out.println(
          ", welcome to our pub.");
                  
          return result;
              }

              
              
          public static void main(String[] args)
              
          {
                  HelloSpeakerPro helloSpeakerPro 
          = new HelloSpeakerPro();
                  HelloSpeaker helloSpeaker 
          = (HelloSpeaker) helloSpeakerPro
                          .getProxy(HelloSpeaker.
          class);
                  helloSpeaker.hello(
          "Huy Vanpon");
              }

              
          }


          posted on 2008-04-13 00:07 HUIKK 閱讀(374) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 兰西县| 锡林郭勒盟| 齐河县| 六安市| 巴林右旗| 昭平县| 邵阳县| 蚌埠市| 文化| 中江县| 建德市| 屯留县| 衡阳县| 新巴尔虎右旗| 辽阳市| 桑植县| 门源| 巴林左旗| 溆浦县| 耒阳市| 南岸区| 天峻县| 奎屯市| 黑水县| 古蔺县| 永川市| 承德市| 肥乡县| 乐都县| 广元市| 会昌县| 托克托县| 凤山市| 石渠县| 章丘市| 克拉玛依市| 巴中市| 绥滨县| 沙洋县| 庆安县| 连平县|