在Struts2中有拦截器的概念,通过它的拦截器可以拦截Action。Struts2的拦截器是通过AOP来实现的,在Spring也有类似的概念。下面的我们先来比较一下Struts2和Spring中AOP的东西。

AOP概念 Struts2 Spring
JoinPoint Action中方法的执行 符合条件方法的执行
Pointcut Action(不能自己指定) 可以通过正则或AspectJ表达式来指定
Advice Before、After、Around Before、After、Around
Aspect 拦截器 拦截器

从上面的比较中可以看到,Struts2的AOP功能比较单一,只能拦截Action类中的方法。Spring的AOP是通过JDK动态代理或者CGLib来生成目标对象的代理对象,然后将增强功能(Aspect【包括了Advice和Pointcut】)织入到符合条件(Pointcut)的类的方法(JoinPoint)上。

Struts2的AOP实现跟Filter的实现差不多,它有一系列的拦截器,称为拦截器栈,通过这些拦截器栈通过ActionInvocation的调度可以在Action中方法执行之前或执行做一些操作。原理图如下:


       下面,用代码来模拟一下它的实现

/*** Action、Interceptor调度器* @author zyb* @since 2013-6-2 下午1:22:05*/public interface ActionInvocation {String invoke();}
/*** Action、Interceptor调度器默认实现* @author zyb* @since 2013-6-2 下午1:21:14*/public class DefaultActionInvoation implements ActionInvocation {private int index;private Action action;private List<Interceptor> interceptors = new ArrayList<Interceptor>();public void addInterceptor(Interceptor interceptor) {this.interceptors.add(interceptor);}public void setAction(Action action) {this.action = action;}@Overridepublic String invoke() {String result = "";// 如果所有的拦截器已经执行完,则执行Action中的方法if (index == interceptors.size()) {result = action.execute();} else {Interceptor interceptor = interceptors.get(index);index++;result = interceptor.intercept(this);}return result;}}
/*** 拦截器* @author zyb* @since 2013-6-2 下午1:23:35*/public interface Interceptor {String intercept(ActionInvocation invocation);}
public class ExceptionInterceptor implements Interceptor {@Overridepublic String intercept(ActionInvocation invocation) {System.out.println("ExceptionInterceptor");return invocation.invoke();}}
public class I18NInterceptor implements Interceptor {@Overridepublic String intercept(ActionInvocation invocation) {System.out.println("I18NInterceptor");return invocation.invoke();}}
/*** 环绕拦截器* @author zyb* @since 2013-6-2 下午1:23:25*/public class AroundInterceptor implements Interceptor {@Overridepublic String intercept(ActionInvocation invocation) {System.out.println("before:" + this.getClass());String result = invocation.invoke();System.out.println("after:" + this.getClass());return result;}}
/*** 环绕拦截器* @author zyb* @since 2013-6-2 下午1:21:24*/public class AroundInterceptor01 implements Interceptor {@Overridepublic String intercept(ActionInvocation invocation) {System.out.println("before:" + this.getClass());String result = invocation.invoke();System.out.println("after:" + this.getClass());return result;}}
/*** Action接口* @author zyb* @since 2013-6-2 下午1:21:55*/public interface Action {String execute();}
public class MyAction implements Action {@Overridepublic String execute() {System.out.println("execute...");return "success:" + getClass();}}
public class Test {public static void main(String[] args) {Interceptor exptionInterceptor = new ExceptionInterceptor();Interceptor i18nInterceptor = new I18NInterceptor();Interceptor aroundInterceptor = new AroundInterceptor();Interceptor aroundInterceptor01 = new AroundInterceptor01();DefaultActionInvoation actionInvocation = new DefaultActionInvoation();actionInvocation.addInterceptor(exptionInterceptor);actionInvocation.addInterceptor(i18nInterceptor);actionInvocation.addInterceptor(aroundInterceptor);actionInvocation.addInterceptor(aroundInterceptor01);Action action = new MyAction();actionInvocation.setAction(action);String result = actionInvocation.invoke();System.out.println("Action result:" + result);}}

Struts2就是这样实现AOP的,比起Spring的AOP实现,简单多了。^_^

大小: 44.3 KB查看图片

模拟Struts2的AOP实现的更多相关文章

  1. 简单模拟struts2及struts2的处理流程介绍

    用了几天模拟struts2,最后结果还是很成功的,也基本没有什么遇上比较难解决的问题,万事开头难,在最开始的时候无从下手,看着下面这张struts2工作流程图配合着网上的博客看了一天终于有了眉目. 看 ...

  2. 简单模拟struts2核心控制器利用反射原理实现参数传递和物理视图跳转

    在能够运用struts2框架进行简单网站开发后,对struts2的一些较原框架强大的功能希望有更深刻的理解,于是尝试从底层开始摸索,本文就在重新学习struts2后,利用简单代码对核心控制器的主要功能 ...

  3. 用js模拟struts2的多action调用

    近期修了几个struts2.1升级到2.3后动态方法调用失效的bug,深有感悟, 原始方法能够參考我之前的博文:struts2.1升级到2.3后动态调用方法问题 可是我那种原始方法有一个局限,就是在s ...

  4. 模拟Struts2框架Action的实现

    1.具体项目结构如下:

  5. 模拟struts2

    利用到的技术:dom4j和xpath 自己写一个Filter 在doFilter中拦截请求 // 2.1 得到请求资源路径            String uri = request.getReq ...

  6. 使用反射模拟struts2属性注入功能

    1.在项目开发中,如果没有使用框架进行数据绑定与封装,则可能会写大量的类似下面的代码: String value=request.getParameter("v"); if(nul ...

  7. Struts2拦截器模拟

    前言: 接触Struts2已经有一段时间,Student核心内容就是通过拦截器对接Action,实现View层的控制跳转.本文根据自身理解对Struts2进行一个Java实例的模拟,方便大家理解! 示 ...

  8. Struts2 源码分析——拦截器的机制

    本章简言 上一章讲到关于action代理类的工作.即是如何去找对应的action配置信息,并执行action类的实例.而这一章笔者将讲到在执行action需要用到的拦截器.为什么要讲拦截器呢?可以这样 ...

  9. struts2框架快速入门小案例

    struts2快速入门: index.jsp------>HelloAction--------->hello.jsp struts2流程 1.导入jar包 struts2的目录结构: a ...

随机推荐

  1. 批量 GBK 转 UTF8 java

    package encoding; import java.io.File; import java.io.IOException; import java.util.Collection; impo ...

  2. Word2007中如何插入参考文献

    很多国内的期刊杂志都只能使用word模板,导致插入参考文献成了件麻烦事,这时特别怀念Latex的便捷.于是找到一篇介绍word2007里插入参考文献的好方法,就是利用尾注的方法使文章的参考文献标号可以 ...

  3. step_by_step_G+入门-在线服务

    第一步:先大概介绍下我们的窗体的布局框架,窗体大体分为以下3大块: 顶部:也就是大的模块划分(比如首页,软件管家,在线服务等) 内容区域:根据选择的不同的顶部模块,进行不同的内容展示: 底部:设置,下 ...

  4. ImageMagick还是GraphicsMagick?

    引自:http://co63oc.blog.51cto.com/904636/328997 ImageMagick(IM) 套装包含的命令行图形工具是一主要自由软件:Linux,其他类Unix操作系统 ...

  5. weblogic的ejb远程调用

    这是一篇对EJB远程调用的简单范例.      1.环境:win7  + weblogic 12c + myeclipse8.5      2.目的:实现在myeclispe中对weblogic中EJ ...

  6. [爬虫]通过url获取连接地址中的数据

    1. 要想获取指定连接的数据,那么就得使用HtmlDocument对象,要想使用HtmlDocument对象就必需引用using HtmlAgilityPack; 2. 详细步骤如下:     步骤一 ...

  7. SQL 注入与防御实例

    注入 1. 创建存储 USE TSQL2012; GO IF OBJECT_ID('Sales.ListCustomersByAddress') IS NOT NULL DROP PROCEDURE ...

  8. C# Best Practices - Specify Clear Method Parameters

    Improve parameters parameter order public OperationResult PlaceOrder(Product product, int quantity, ...

  9. C#变量命名规范

    1.1命名 1.  所有命名必须有意义 2.  成员变量声明在类的顶端,并且每个变量一行 3.  局部变量声明在引用之前 1.1.1  常量命名 1.  常量名用全大写:MAX_PARAMETER_C ...

  10. seajs + easyui [转]

    * *content seajs+easyui使用 */ /** * 首先来看看在seajs中jquery和jquery插件如何使用 */ 1.jquery.js define(function(re ...