[摘錄]動態代理

          摘錄地址:http://jamesby.javaeye.com/blog/71036

          用Spring大概有3個月,一直都是張口AOP,閉口AOP,但是Spring的AOP具體是怎么實現的,現在也不是十分清楚.

          只知道可以用JDK的動態代理,基于接口,或者是cglib方式字節碼增強.目前對JDK的動態代理方式不是很理解,固拿出來探討下,我知道這應該是個新手貼,但還是請大家給點意見.如果真正理解了動態代理,那么對基于Spring AOP出現的各種現象就比較容易理解了.

          先貼上一段代碼.

          代碼
          1. package com.jamesby.proxy;   
          2. public interface CarCompany {   
          3.   
          4.     public void makeCar();    
          5.     public void abc();   
          6. }   
          7.   
          8. package com.jamesby.proxy;   
          9.   
          10. public class CarCompanyImpl implements CarCompany {   
          11.     public void makeCar() {   
          12.         System.out.println("Company A make a car!");   
          13.         abc();   
          14.     }   
          15.     public void abc()   
          16.     {   
          17.         System.out.println("abc is invoked!");   
          18.     }   
          19. }   
          20.   
          21. package com.jamesby.proxy;   
          22.   
          23. import java.lang.reflect.InvocationHandler;   
          24. import java.lang.reflect.Method;   
          25.   
          26. public class CarCompanyHandler implements InvocationHandler  {   
          27.     CarCompany com;   
          28.     //構造時將被代理類綁定到Handler
          29.     public CarCompanyHandler(CarCompany com) {   
          30.         this.com = com;   
          31.     }   
          32.      
          33.   /**相當于要把代理類的執行方法重新包裝
          34.      *proxy:被代理類
          35.      *method:被代理類要執行的方法
          36.      *args:被代理類要執行的方法時需要的參數
          37.    **/
          38.     public Object invoke(Object proxy, Method method, Object[] args)   
          39.             throws Throwable {   
          40.             System.out.println("time1:" + System.currentTimeMillis());   
          41.             method.invoke(this.com, new Class[] {});   
          42.             System.out.println("time2:" + System.currentTimeMillis());   
          43.         return null;   
          44.     }   
          45. }   
          46.   
          47. package com.jamesby.proxy;   
          48.   
          49. import java.lang.reflect.Proxy;   
          50.   
          51. public class Test {   
          52.     public static void main(String[] arg) {   
          53.         CarCompanyImpl a = new CarCompanyImpl();   
          54.         //將被代理類綁定到handler 
          55.         CarCompanyHandler handler = new CarCompanyHandler(a);   
          56.         // 產生一個新的代理類 ,將handler綁定到代理類中  
          57.         CarCompany com = (CarCompany) Proxy.newProxyInstance(Test.class  
          58.                 .getClassLoader(), new Class[] { CarCompany.class }, handler);   
          59.         com.makeCar();   
          60.     }   
          61. }  



          歡迎大家訪問我的個人網站 萌萌的IT人

          posted on 2006-06-01 11:20 見酒就暈 閱讀(92) 評論(0)  編輯  收藏 所屬分類: J2EE文章

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(3)

          我參與的團隊

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          BLOG

          FRIENDS

          LIFE

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 阿巴嘎旗| 宁乡县| 东源县| 贺兰县| 牡丹江市| 锦州市| 宁波市| 旬邑县| 三明市| 德惠市| 台东县| 武清区| 安庆市| 涿鹿县| 仙游县| 海宁市| 临沧市| 荆州市| 施秉县| 泽普县| 岢岚县| 濮阳市| 文成县| 大田县| 新营市| 柳江县| 连城县| 温泉县| 柯坪县| 米泉市| 黄龙县| 虹口区| 谢通门县| 濮阳县| 汉源县| 阿合奇县| 肥城市| 平南县| 德格县| 山西省| 临颍县|