城市獵人

          在一網情深的日子里,誰能說得清是苦是甜,只知道確定了就義無反顧
          posts - 1, comments - 7, trackbacks - 0, articles - 89

          AOP之靜態代理和動態代理

          Posted on 2009-08-02 11:31 sailor 閱讀(922) 評論(2)  編輯  收藏 所屬分類: spring
          一、靜態代理:
              靜態代理要求代理對象和被代理對象實現同一個對象。

          IUserService:
          package com.swjs.aop.serivce;

          import java.util.List;

          /**
           * 
          @author jason
           *
           
          */

          public interface IUserService {
              List getUserList();
          }


          被代理類UserService:
          package com.swjs.aop.serivce;

          import java.util.List;

          /**
           * 
          @author jason
           *
           
          */

          public class UserService implements IUserService {

              
          public List getUserList() {
                  System.out.println(
          "get Userlist");
                  
          return null;
              }


          }

          代理類:
          package com.swjs.aop.serivce;

          import java.util.List;

          /**
           * 
          @author jason
           *
           
          */

          public class SecureProxy implements IUserService{

              
          private IUserService userService;

              
          public SecureProxy(IUserService userService) {
                  
          super();
                  
          this.userService = userService;
              }

              
              
          public List getUserList()
              
          {
                  System.out.println(
          "身份檢查");
                  userService.getUserList();
                  System.out.println(
          "事務提交");
                  
          return null;
              }

          }

          測試類:
          package com.swjs.aop.serivce;

          /**
           * 
          @author jason
           *
           
          */

          public class StaticProxyTest {

              
          /**
               * 
          @param args
               
          */

              
          public static void main(String[] args) {
                  IUserService userService 
          = new UserService();
                  SecureProxy proxy 
          = new SecureProxy(userService);
                  proxy.getUserList();
              }

          }


          顯示結果:
          身份檢查
          get Userlist
          事務提交

          二:動態代理
          接口
          public interface IUserService {

              
          public void getUserList(int start, int limit);
          }


          業務方法:
          public class UserService implements IUserService {

              
          public void getUserList(int start, int limit) {
                  System.out.println(
          "start: " + start + " limit: " + limit );
                  System.out.println(
          "get user List");
              }

          }

          處理器:
          public class SecureHandler implements InvocationHandler {

              
          private IUserService service;
              
              
              
          public SecureHandler(IUserService service) {
                  
          super();
                  
          this.service = service;
              }



              
          public Object invoke(Object proxy, Method method, Object[] arg)
                      
          throws Throwable {
                  System.out.println(
          "trac begin");
                  System.out.println(arg.length);
                  
          for(int i = 0; i <  arg.length; i++)
                  
          {
                      System.out.println(arg[i]);
                  }

                  method.invoke(service, arg);
                  System.out.println(
          "trac end");
                  
          return null;
              }


          }


          測試類:
          public class DynamicProxyTest {

              
          public static void main(String[] args)
              
          {
                  IUserService service 
          = new UserService();
                  SecureHandler handler 
          = new SecureHandler(service);
                  IUserService serv 
          = (IUserService)Proxy.newProxyInstance(service.getClass().getClassLoader(), 
                                  service.getClass().getInterfaces(), handler);
                  serv.getUserList(
          0,10);
              }

          }

          顯示結果:
          trac begin
          2
          0
          10
          start: 
          0 limit: 10
          get user List
          trac end

          Feedback

          # re: AOP之靜態代理和動態代理  回復  更多評論   

          2014-12-05 17:03 by AloneAli
          實質就是裝飾者模式?

          # re: AOP之靜態代理和動態代理  回復  更多評論   

          2014-12-05 17:11 by AloneAli
          @AloneAli不好意思,弄錯了。代理模式是種模式。。。不是裝飾者模式。
          主站蜘蛛池模板: 瑞昌市| 连江县| 洛南县| 黄骅市| 聂荣县| 厦门市| 阿拉善盟| 孟村| 寿宁县| 长寿区| 池州市| 连州市| 离岛区| 文安县| 华安县| 当阳市| 囊谦县| 昆山市| 佛教| 大安市| 溧水县| 社旗县| 安仁县| 河源市| 达州市| 红原县| 鹤壁市| 南昌县| 云林县| 毕节市| 泸水县| 南康市| 达孜县| 海林市| 壤塘县| 宁陕县| 吴桥县| 东海县| 通城县| 新营市| 扶沟县|