模拟Struts2的AOP实现
在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实现的更多相关文章
- 简单模拟struts2及struts2的处理流程介绍
用了几天模拟struts2,最后结果还是很成功的,也基本没有什么遇上比较难解决的问题,万事开头难,在最开始的时候无从下手,看着下面这张struts2工作流程图配合着网上的博客看了一天终于有了眉目. 看 ...
- 简单模拟struts2核心控制器利用反射原理实现参数传递和物理视图跳转
在能够运用struts2框架进行简单网站开发后,对struts2的一些较原框架强大的功能希望有更深刻的理解,于是尝试从底层开始摸索,本文就在重新学习struts2后,利用简单代码对核心控制器的主要功能 ...
- 用js模拟struts2的多action调用
近期修了几个struts2.1升级到2.3后动态方法调用失效的bug,深有感悟, 原始方法能够參考我之前的博文:struts2.1升级到2.3后动态调用方法问题 可是我那种原始方法有一个局限,就是在s ...
- 模拟Struts2框架Action的实现
1.具体项目结构如下:
- 模拟struts2
利用到的技术:dom4j和xpath 自己写一个Filter 在doFilter中拦截请求 // 2.1 得到请求资源路径 String uri = request.getReq ...
- 使用反射模拟struts2属性注入功能
1.在项目开发中,如果没有使用框架进行数据绑定与封装,则可能会写大量的类似下面的代码: String value=request.getParameter("v"); if(nul ...
- Struts2拦截器模拟
前言: 接触Struts2已经有一段时间,Student核心内容就是通过拦截器对接Action,实现View层的控制跳转.本文根据自身理解对Struts2进行一个Java实例的模拟,方便大家理解! 示 ...
- Struts2 源码分析——拦截器的机制
本章简言 上一章讲到关于action代理类的工作.即是如何去找对应的action配置信息,并执行action类的实例.而这一章笔者将讲到在执行action需要用到的拦截器.为什么要讲拦截器呢?可以这样 ...
- struts2框架快速入门小案例
struts2快速入门: index.jsp------>HelloAction--------->hello.jsp struts2流程 1.导入jar包 struts2的目录结构: a ...
随机推荐
- 安装VMware vSphere 的目的就是在一台物理服务器上安装很多很多的虚拟机
版权声明:本文为博主原创文章,未经博主允许不得转载. 我们安装VMware vSphere 的目的就是在一台物理服务器上安装很多很多的虚拟机,我们可以通过VMware vSphere Client直接 ...
- 帝国cms7.2自定义列表建立tag效果 代码 教程
统计记录:(如:select count(*) as total from phome_ecms_news where classid=1 and checked=1) 注:这句SQL的意思是查找统计 ...
- Qt之美(一):d指针/p指针详解
Translated by mznewfacer 2011.11.16 首先,看了Xizhi Zhu 的这篇Qt之美(一):D指针/私有实现,对于很多批评不美的同路人,暂且不去评论,只是想支持 ...
- UITextField align left margin
如果我们想让我们的UITextField输入的字体偏移几个像素,我们常常用空格来代替,有时候我们不想用空格的话怎么办? #import <UIKit/UIKit.h> @interface ...
- 1043 - Triangle Partitioning(数学)
1043 - Triangle Partitioning PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit ...
- Python之美[从菜鸟到高手]--一步一步动手给Python写扩展(异常处理和引用计数)
我们将继续一步一步动手给Python写扩展,通过上一篇我们学习了如何写扩展,本篇将介绍一些高级话题,如异常,引用计数问题等.强烈建议先看上一篇,Python之美[从菜鸟到高手]--一步一步动手给Pyt ...
- SVNserver的本地搭建和使用
Subversion是优秀的版本号控制工具,其具体的的长处和具体介绍,这里就不再多说. 首先来下载和搭建SVNserver. 如今Subversion已经迁移到apache站点上了,下载地址: htt ...
- 禁用Visual Studio 2013的Browser Link功能
禁用Visual Studio 2013的Browser Link功能 GET http://localhost:37478/7fd25f8af33f443494e765be19be6240/brow ...
- 子请求执行失败。有关更多信息,请检查 InnerException。
异常:子请求执行失败.有关更多信息,请检查 InnerException. 错误:程序请求异常 可能原因: 1.可以检查是否引用了分布视图,而分布视图中发生错误 2.可以检查是否引用了分布视图,而分布 ...
- Definitions
Definitions and ODR Definitions are declarations that fully define the entity introduced by the decl ...