隨筆-57  評論-202  文章-17  trackbacks-0
                在JDK1.3版本中引入了Dynamic Proxy的代理機制,通過實現java.lang.reflect.InvocationHandler接口,可以實現攔截需要改寫的方法。下面是一個簡單范例。
                有下面一個接口TestInterface和它的一個實現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方法,調用自己的實現,這需要實現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;
            
            
          /**
             * 將動態代理綁定到指定的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 小米 閱讀(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);就不行,這兩個應該應用同一個對象吧,請教
            回復  更多評論
            
          # re: JDK Dynamic Proxy模式的簡單范例 2005-12-08 11:11 | 寂寞邀請
          操作順序 client->proxy->this.test->實際的操作

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

          client->proxy->proxy->proxy...........  回復  更多評論
            
          # re: JDK Dynamic Proxy模式的簡單范例[未登錄] 2011-06-20 17:40 | fox
          簡單,明了  回復  更多評論
            
          主站蜘蛛池模板: 新昌县| 镇江市| 英吉沙县| 东至县| 无棣县| 通海县| 兴山县| 白银市| 闻喜县| 宣武区| 扎兰屯市| 个旧市| 岚皋县| 禹州市| 阜阳市| 霸州市| 彭阳县| 咸丰县| 阜康市| 金湖县| 城市| 汉寿县| 石阡县| 吉水县| 永善县| 虎林市| 定南县| 昆明市| 阳高县| 萍乡市| 郯城县| 新源县| 西和县| 廊坊市| 怀柔区| 屏东市| 岳池县| 施秉县| 庐江县| 崇文区| 巴南区|