隨筆-57  評論-202  文章-17  trackbacks-0
                在JDK1.3版本中引入了Dynamic Proxy的代理機制,通過實現(xiàn)java.lang.reflect.InvocationHandler接口,可以實現(xiàn)攔截需要改寫的方法。下面是一個簡單范例。
                有下面一個接口TestInterface和它的一個實現(xiàn)TestImpl:

          package sample.proxy;

          /**
           * <p>Title: </p>
           *
           * <p>Description: </p>
           *
           * <p>Copyright: Copyright (c) 2005</p>
           *
           * <p>Company: </p>
           *
           * @author George Hill
           * @version 1.0
           
          */


          public interface TestInterface {

            
          public String print();

          }


          package sample.proxy;

          /**
           * <p>Title: </p>
           *
           * <p>Description: </p>
           *
           * <p>Copyright: Copyright (c) 2005</p>
           *
           * <p>Company: </p>
           *
           * @author George Hill
           * @version 1.0
           
          */


          public class TestImpl implements TestInterface {
            
            
          public String print() {
              
          return "Hello, it's from TestImpl class";
            }

            
          }


                下面攔截print方法,調用自己的實現(xiàn),這需要實現(xiàn)java.lang.reflect.InvocationHandler接口。

          package sample.proxy;

          import java.lang.reflect.
          *;

          /**
           * <p>Title: </p>
           *
           * <p>Description: </p>
           *
           * <p>Copyright: Copyright (c) 2005</p>
           *
           * <p>Company: </p>
           *
           * @author George Hill
           * @version 1.0
           
          */


          public class TestHandler implements InvocationHandler {
            
            TestInterface test;
            
            
          /**
             * 將動態(tài)代理綁定到指定的TestInterface
             * @param test TestInterface
             * @return TestInterface 綁定代理后的TestInterface
             
          */

            
          public TestInterface bind(TestInterface test) {
              
          this.test = test;
              
              TestInterface proxyTest 
          = (TestInterface) Proxy.newProxyInstance(
                test.getClass().getClassLoader(), test.getClass().getInterfaces(), 
          this);
              
              
          return proxyTest;
            }

            
            
          /**
             * 方法調用攔截器,攔截print方法
             * @param proxy Object
             * @param method Method
             * @param args Object[]
             * @return Object
             * @throws Throwable
             
          */

            
          public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
              
          // 如果調用的是print方法,則替換掉
              if ("print".equals(method.getName())) {
                
          return "HaHa, It's come from TestHandler";
              }
           else {
                
          return method.invoke(this.test, args);
              }

            }

            
          }


                下面是測試用例:

          package sample.test;

          import junit.framework.
          *;

          import sample.proxy.
          *;

          /**
           * <p>Title: </p> 
           * 
           * <p>Description: </p> 
           * 
           * <p>Copyright: Copyright (c) 2005</p> 
           * 
           * <p>Company: </p>
           * 
           * @author George Hill
           * @version 1.0
           
          */


          public class TestDynamicProxy extends TestCase {
            
            
          private TestInterface test = null;

            
          protected void setUp() throws Exception {
              super.setUp();
              TestHandler handler 
          = new TestHandler();
              
          // 用handler去生成實例
              test = handler.bind(new TestImpl());
            }


            
          protected void tearDown() throws Exception {
              test 
          = null;
              super.tearDown();
            }


            
          public void testPrint() {
              System.
          out.println(test.print());
            }


          }


                運行測試用例,可以看到輸出的是“HaHa, It's come from TestHandler”。
          posted on 2005-05-24 17:47 小米 閱讀(3509) 評論(3)  編輯  收藏 所屬分類: Java

          評論:
          # re: JDK Dynamic Proxy模式的簡單范例 2005-10-20 13:10 | 鳥不生蛋蛋的地方
          if ("print".equals(method.getName())) {
          return "HaHa, It's come from TestHandler";
          } else {
          return method.invoke(this.test, args);

          為什么這里的返回改成return method.invoke(proxy, args);就不行,這兩個應該應用同一個對象吧,請教
            回復  更多評論
            
          # re: JDK Dynamic Proxy模式的簡單范例 2005-12-08 11:11 | 寂寞邀請
          操作順序 client->proxy->this.test->實際的操作

          換成return method.invoke(proxy, args),就死循環(huán)了。

          client->proxy->proxy->proxy...........  回復  更多評論
            
          # re: JDK Dynamic Proxy模式的簡單范例[未登錄] 2011-06-20 17:40 | fox
          簡單,明了  回復  更多評論
            
          主站蜘蛛池模板: 犍为县| 安达市| 兴安盟| 昭苏县| 岳普湖县| 牟定县| 贡嘎县| 修武县| 江北区| 嘉善县| 洛隆县| 榆树市| 项城市| 东宁县| 宜阳县| 社旗县| 巴楚县| 新竹县| 昌江| 台北县| 安岳县| 芦山县| 南雄市| 龙口市| 南澳县| 辽中县| 马边| 东方市| 南城县| 南涧| 合川市| 中阳县| 景洪市| 海盐县| 登封市| 唐海县| 河源市| 万盛区| 锦屏县| 正宁县| 哈尔滨市|