城市獵人

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

          導航

          <2014年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(3)

          文章分類(90)

          文章檔案(90)

          收藏夾(12)

          搜索

          •  

          最新評論

          AOP之靜態(tài)代理和動態(tài)代理

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

          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();
              }

          }


          顯示結(jié)果:
          身份檢查
          get Userlist
          事務提交

          二:動態(tài)代理
          接口
          public interface IUserService {

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


          業(yè)務方法:
          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);
              }

          }

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

          Feedback

          # re: AOP之靜態(tài)代理和動態(tài)代理  回復  更多評論   

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

          # re: AOP之靜態(tài)代理和動態(tài)代理  回復  更多評論   

          2014-12-05 17:11 by AloneAli
          @AloneAli不好意思,弄錯了。代理模式是種模式。。。不是裝飾者模式。
          主站蜘蛛池模板: 黑河市| 获嘉县| 屏南县| 彭州市| 昔阳县| 四会市| 涡阳县| 烟台市| 剑川县| 房山区| 界首市| 遂平县| 定远县| 肇源县| 景东| 邓州市| 余江县| 双峰县| 安阳市| 镇平县| 鄂伦春自治旗| 嫩江县| 余姚市| 文水县| 石景山区| 满城县| 蒲江县| 平遥县| 东兴市| 繁昌县| 富宁县| 壶关县| 潢川县| 兰州市| 灵丘县| 洮南市| 青岛市| 中江县| 仁寿县| 中宁县| 东城区|