在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. WebAppScaner

    https://www.ohloh.net/p/simple-scan/ https://code.google.com/p/skipfish/ http://code.google.com/p/wa ...

  2. JAVA 创建TXT文件,写入文件内容,读取文件内容

    [java]  view plain copy   package com.abin.facade.ws.mail.function; import java.io.BufferedReader; i ...

  3. Objective-c 访问控制

    在Objective-c中定义类时,也可以使用类似于C++中public.private来修饰成员变量,如下: @intterface Test:NSObject{ @public int i; in ...

  4. Android SDK三种更新失败及其解决方法

    更新Android SDK,居然失败了三次. 1.第一次失败 出现Failed to fetch URL错误提示 Failed to fetch URL https://dl-ssl.google.c ...

  5. Ext JS学习第六天 Ext_window组件(三)

    此文用来记录学习笔记: 今天再来一个例子巩固一下学习的window: –例2: 在window中添加子组件,并讲解常用查找组件的方式: •重点分析:该实例主要针对于组件的查找进行详细的讲解,在以后的应 ...

  6. .net 中文显示乱码问题(Chinese display with messy code)

    Case:同样的代码,本地开发环境(local is Chinese Simplify)可以成功运行,但是放到Windows Server 2008 R2(Local is United State) ...

  7. Informatica 9.5.1 安装配置

    Informatica  结构 1个或多个资源库(Respository) PowerCenter数据整合引擎是基于元数据驱动的,提供了基于数据驱动的元数据知识库(Repository),该元数据知识 ...

  8. python之高阶函数编程

    在这篇文章中我指出两点: 第一:系统函数可以被覆盖 比如: a=abs(-10) print a 10 但是,如果把函数本身赋值给变量呢? f = abs f <built-in functio ...

  9. BZOJ 2748 音量调节

           这道题我开始做时想用搜索来做,但是失败了,后来仔细一想发现这就是一个背包问题,之后一切就简单多了.        代码如下: #include<cstdio> #includ ...

  10. JavaSE学习总结第14天_API常用对象4

      14.01 如何校验一个QQ号码案例 import java.util.Scanner; /* * 校验qq号码. * 1:要求必须是5-15位数字 * 2:0不能开头 * * 分析: * A:键 ...