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

          2009年2月18日

          Java靜態(tài)代理和動態(tài)代理

          網上和bolg上相關例子不少,今天自己動手寫了個例子作為學習筆記。
          首先java代理分為靜態(tài)代理和動態(tài)代理,動態(tài)代理中java提供的動態(tài)代理需要動態(tài)代理一個inteface,如果沒有inteface則需要使用實現cglib提供的接口。
          下面例子只實現動態(tài)代理
          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的才執(zhí)行(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();
              }

              
          }

          執(zhí)行結果
          method1
          cglib proxy
          method2

          posted @ 2009-02-18 16:52 Pitey 閱讀(495) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 南澳县| 新丰县| 双柏县| 克拉玛依市| 平邑县| 邹平县| 庄浪县| 晋州市| 绥芬河市| 南川市| 泾源县| 日喀则市| 鄯善县| 吉木萨尔县| 菏泽市| 桦南县| 桦川县| 西乌| 蒙山县| 宜黄县| 镇宁| 文山县| 洛阳市| 芮城县| 景洪市| 中牟县| 藁城市| 日照市| 榆树市| 博爱县| 邹城市| 邯郸市| 始兴县| 安宁市| 涞源县| 阳高县| 如东县| 宁国市| 北安市| 富阳市| 东辽县|