隨筆 - 8, 文章 - 0, 評論 - 4, 引用 - 0
          數據加載中……

          Java靜態代理和動態代理

          網上和bolg上相關例子不少,今天自己動手寫了個例子作為學習筆記。
          首先java代理分為靜態代理和動態代理,動態代理中java提供的動態代理需要動態代理一個inteface,如果沒有inteface則需要使用實現cglib提供的接口。
          下面例子只實現動態代理
          public class MyProxy implements InvocationHandler{
              
              
          static Object proxyObj = null;    
              
              
          public static Object getInstance(Object obj){
                  proxyObj
          = obj;
                  
          return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new MyProxy());
              }

              
              
          public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                  System.out.println(
          "使用代理..");
                  
          return method.invoke(proxyObj, args);
              }

          }

          實現方式
          public static void main(String[] args) {
                  ILeaveService service 
          = (ILeaveService)MyProxy.getInstance(new LeaveServiceImpl());
                  
          try {
                      service.listBizObjByHql(
          "");
                  }

                  
          catch (ServiceException e) {
                      e.printStackTrace();
                  }

              }

          打印出:
          使用代理..
          query Hql..
          使用Cglib
          public class Test2 implements MethodInterceptor {
                  
              
              
          public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                  System.out.println(
          "cglib proxy");
                  
          return methodProxy.invokeSuper(obj, args);
              }

              
          }

          實現
          public static void main(String[] args) {
                  Enhancer enhancer 
          = new  Enhancer();
                  enhancer.setSuperclass(Test3.class);
                  enhancer.setCallback(new Test2());        

                  Test3 test 
          = (Test3)enhancer.create();
                  test.prinInfo(
          "p.");
              }
          輸出
          cglib proxy
          p.
          過濾器
          public class Test4 implements CallbackFilter{

              
          public int accept(Method method) {
                  
          if(method.getName().equals("method1")){
                      
          return 1;
                  }
          else if(method.getName().equals("method2")){
                      
          return 0;
                  }

                  
          return 0;
              }

              
          }
          只有返回0的才執行(cglib中的NoOp.INSTANCE就是一個空的攔截器
              public static void main(String[] args) {        
                  Callback [] callbacks 
          = new Callback[]{new Test2(),NoOp.INSTANCE};
                  Enhancer enhancer 
          = new Enhancer();
                  enhancer.setSuperclass(Test3.
          class);
                  enhancer.setCallbacks(callbacks);
                  enhancer.setCallbackFilter(
          new Test4());
                  Test3 test3 
          = (Test3)enhancer.create();
                  test3.method1();
                  test3.method2();
              }

              
          }

          執行結果
          method1
          cglib proxy
          method2

          posted on 2009-02-18 16:52 Pitey 閱讀(494) 評論(0)  編輯  收藏 所屬分類: J2EE


          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 从化市| 汶上县| 福贡县| 合江县| 巴彦县| 东源县| 铜川市| 达尔| 东城区| 阿克| 西盟| 读书| 天气| 沙田区| 台湾省| 青海省| 安泽县| 民勤县| 策勒县| 汝城县| 沽源县| 宁蒗| 江孜县| 余江县| 尼勒克县| 平果县| 宁河县| 祁阳县| 东乡族自治县| 靖州| 福清市| 保康县| 宁陕县| 教育| 同江市| 云和县| 桑植县| 广东省| 莱州市| 磐安县| 松阳县|