Interceptor实例

来源:互联网 发布:windows常用端口 编辑:程序博客网 时间:2024/06/05 08:42

转自:http://www.iteye.com/topic/1121396

1.什么是拦截器: 

拦截器是动态拦截Action调用的对象。它提供了一种机制使得开发者可以定义action执行之前或之后执行的代码,也可以在一个action执行前阻止其执行。 

2.AOP: 
提到拦截器,我们不得不提到AOP. 
AOP(Aspect-Oriented Programming)译为:“面向切面编程”或者“面向方面编程”。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。拦截器的就是实现AOP的一种策略。 

拦截器的工作原理简略图: 


3.拦截器的作用: 
  我们可以用Interceptor在Action的方法执行之前或者之后做一些处理,struts的核心功能正是由拦截器来实现的,比如捕获异常、数据校验、安全审查等等。 

4.拦截器的工作原理: 
Interceptor Stack(拦截器堆)中有顺序的存储着多个Interceptor,他们联接成链状,然后按照添加的顺序,依次调用。这里用到了递归调用,我认为这是设计者的聪明之处。 
DefaultActionInvocation类持有拦截器链的引用,以及action的引用,是控制递归调用的重要类。 
关于递归更深入的探讨,请猛击:http://candy-code.iteye.com/blog/1443427 
下面我们就来模拟一下Interceptor的工作原理 
5.Interceptor模拟: 

Invocation.java 



Java代码  收藏代码
  1.     
  2. 1.package com.interceptor.test;    
  3. 2.    
  4. 3.import java.util.ArrayList;    
  5. 4.import java.util.Iterator;    
  6. 5.import java.util.List;    
  7. 6./**  
  8. 7. * 模拟DefaultActionInvocation  
  9. 8. *  所有的拦截器存放在一个ArrayList中  
  10. 9. *  该类应当持有拦截器数组的引用和Action的引用  
  11. 10. */    
  12. 11.public class Invocation{    
  13. 12.    private List<Interceptor> interceptors = new ArrayList<Interceptor>();    
  14. 13.    private Iterator<Interceptor> interator;    
  15. 14.    private Action action;    
  16. 15.    /**  
  17. 16.     * 初始化拦截器和action  
  18. 17.     * 用new 模拟  
  19. 18.     * 实际上Invocation是从struts.xml读取内容,并初始化的  
  20. 19.     */    
  21. 20.    public Invocation(){    
  22. 21.        //按顺序加入Interceptor    
  23. 22.        interceptors.add(new FirstInterceptor());    
  24. 23.        interceptors.add(new SecondInterceptor());    
  25. 24.        interceptors.add(new ThirdInterceptor());    
  26. 25.        interator = interceptors.iterator();    
  27. 26.        action = new Action();    
  28. 27.    }    
  29. 28.    /**  
  30. 29.     * 这是一个递归方法  
  31. 30.     * 方法直接或者间接地调用自身即为递归。 
  32. 31.     *invoke()调用intercept(),intercept()又调用invoke()   
  33. 32.     */    
  34. 33.    public void invoke(){    
  35. 34.        Interceptor interceptor;    
  36. 35.        //若链表中仍有Interceptor,则调用下一个Interceptor    
  37. 36.        if(interator.hasNext()){    
  38. 37.            interceptor = interator.next();    
  39. 38.            interceptor.intercept(this);    
  40. 39.        }    
  41. 40.        //链表中没有Interceptor了,则调用Action    
  42. 41.        else{    
  43. 42.            action.execute();    
  44. 43.        }    
  45. 44.    }    
  46. 45.}    


Interceptor.java 
Java代码  收藏代码
  1. 1.package com.interceptor.test;    
  2. 2.//模拟com.opensymphony.xwork2.interceptor.Interceptor接口    
  3. 3.//所有拦截器都应该实现该接口或者继承自Interceptor的子类    
  4. 4.public interface Interceptor {    
  5. 5.    //这是拦截器类的最重要的方法    
  6. 6.    //invocation用于存储拦截器链表    
  7. 7.    public void intercept(Invocation invocation);    
  8. 8.}    
  9.    
FirstInterceptor.java 
Java代码  收藏代码
  1.     
  2. 1.package com.interceptor.test;    
  3. 2.//第一个拦截器    
  4. 3.public class FirstInterceptor implements Interceptor{    
  5. 4.    @Override    
  6. 5.    public void intercept(Invocation invocation) {    
  7. 6.        //向控制输出信息,来模拟action调用前的处理工作    
  8. 7.        System.out.println("first interceptor -->be called");    
  9. 8.        //回调DefaultAcctionInvocation的方法    
  10. 9.        invocation.invoke();    
  11. 10.        //模拟action调用后的处理工作    
  12. 11.        System.out.println("first interceptor -->return");    
  13. 12.    }    
  14. 13.}    
  15.    

SecondInterceptor.java 
Java代码  收藏代码
  1.    
  2. 1.package com.interceptor.test;    
  3. 2.//第二个拦截器    
  4. 3.public class SecondInterceptor implements Interceptor{    
  5. 4.    @Override    
  6. 5.    public void intercept(Invocation invocation) {    
  7. 6.        System.out.println("Second interceptor -->be called");    
  8. 7.        invocation.invoke();    
  9. 8.        System.out.println("Second interceptor -->return");    
  10. 9.    }    
  11. 10.}    
  12.    

ThirdInterceptor.java 
Java代码  收藏代码
  1.    
  2. 1.package com.interceptor.test;    
  3. 2.//第三个拦截器    
  4. 3.public class ThirdInterceptor implements Interceptor{    
  5. 4.    @Override    
  6. 5.    public void intercept(Invocation invocation) {    
  7. 6.        System.out.println("Third interceptor -->be called");    
  8. 7.        invocation.invoke();    
  9. 8.        System.out.println("Third interceptor -->return");    
  10. 9.    }    
  11. 10.}    
  12.    

Action.java 
Java代码  收藏代码
  1. package com.interceptor.test;  
  2.   
  3. public class Action {  
  4.     public String execute(){  
  5.         System.out.println("Action-->execute");  
  6.         return "success";  
  7.     }  
  8. }  

Main.java 
Java代码  收藏代码
  1. 1.package com.interceptor.test;    
  2. 2.//用于启动模拟程序    
  3. 3.//模拟StrutsActionProxy的execute()方法    
  4. 4.public class Main {    
  5. 5.    // 创建Invocation,并调用其invoke()方法    
  6. 6.    public static void main(String[] args) {    
  7. 7.        new Invocation().invoke();    
  8. 8.    }    
  9. 9.}    
  10.    


控制台输出结果为: 
Java代码  收藏代码
  1.    
  2. 1.first interceptor -->be called    
  3. 2.Second interceptor -->be called    
  4. 3.Third interceptor -->be called    
  5. 4.Action-->execute    
  6. 5.Third interceptor -->return    
  7. 6.Second interceptor -->return    
  8. 7.first interceptor -->return    
  9.    


相信看到输出结果之后,不用过多的解释,你也会对Interceptor的工作原理有更具体的了解了。 

补充:本文中之所以只谈递归,不谈模式,是为了让读者更深刻更具象的了解底层原理。 

0 0
原创粉丝点击