隨筆-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方法,調(diào)用自己的實現(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;
            }

            
            
          /**
             * 方法調(diào)用攔截器,攔截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 {
              
          // 如果調(diào)用的是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 小米 閱讀(3500) 評論(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);就不行,這兩個應(yīng)該應(yīng)用同一個對象吧,請教
            回復(fù)  更多評論
            
          # re: JDK Dynamic Proxy模式的簡單范例 2005-12-08 11:11 | 寂寞邀請
          操作順序 client->proxy->this.test->實際的操作

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

          client->proxy->proxy->proxy...........  回復(fù)  更多評論
            
          # re: JDK Dynamic Proxy模式的簡單范例[未登錄] 2011-06-20 17:40 | fox
          簡單,明了  回復(fù)  更多評論
            
          主站蜘蛛池模板: 颍上县| 凤台县| 浪卡子县| 东平县| 炉霍县| 石台县| 策勒县| 邳州市| SHOW| 磴口县| 溧水县| 龙陵县| 习水县| 景谷| 连山| 永靖县| 来凤县| 浦东新区| 乡宁县| 贡嘎县| 辉县市| 英超| 隆子县| 高安市| 竹北市| 会昌县| 楚雄市| 泽州县| 荆门市| 天气| 怀宁县| 安陆市| 永川市| 盐城市| 布拖县| 雷州市| 神农架林区| 共和县| 高州市| 奉新县| 河间市|