隨筆-57  評(píng)論-202  文章-17  trackbacks-0
                在JDK1.3版本中引入了Dynamic Proxy的代理機(jī)制,通過(guò)實(shí)現(xiàn)java.lang.reflect.InvocationHandler接口,可以實(shí)現(xiàn)攔截需要改寫(xiě)的方法。下面是一個(gè)簡(jiǎn)單范例。
                有下面一個(gè)接口TestInterface和它的一個(gè)實(shí)現(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)用自己的實(shí)現(xiàn),這需要實(shí)現(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;
            
            
          /**
             * 將動(dòng)態(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);
              }

            }

            
          }


                下面是測(cè)試用例:

          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去生成實(shí)例
              test = handler.bind(new TestImpl());
            }


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


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


          }


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

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

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

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

          client->proxy->proxy->proxy...........  回復(fù)  更多評(píng)論
            
          # re: JDK Dynamic Proxy模式的簡(jiǎn)單范例[未登錄](méi) 2011-06-20 17:40 | fox
          簡(jiǎn)單,明了  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 临湘市| 屏东县| 扎鲁特旗| 大悟县| 上林县| 平邑县| 神木县| 定安县| 卢氏县| 贵南县| 临猗县| 连云港市| 丘北县| 西青区| 方正县| 宁德市| 濮阳县| 盘锦市| 巴彦淖尔市| 新泰市| 台南市| 青河县| 蒲城县| 濮阳市| 苏尼特右旗| 西畴县| 兰溪市| 秦安县| 大冶市| 普安县| 辉县市| 太白县| 进贤县| 建水县| 囊谦县| 台东市| 迁西县| 孙吴县| 齐河县| 吉首市| 剑河县|