模拟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 ...
随机推荐
- 百度2017笔试题:寻找n个员工中未打卡的那一个
声明:图片来自网络,笔者只是试着做了一下,然后做个记录. 拿到这个题目的时候,笔者首先想到的是二分.两个数组,一个是全体员工的集合A:一个是缺少一人的集合B.对A,B排序,再对B进行二分,得到B的中间 ...
- ThinkPHP 3.1.2 视图-1
一.模板的使用 (重点) a.规则 模板文件夹下[TPL]/[分组文件夹/][模板主题文件夹/]和模块名同名的文件夹[Index]/和方法名同名的文件 [index].html(.tpl) 更换模板文 ...
- python的Error集,17个新手常见Python运行时错误
python及相关工具安装Error集 . 如果升级python版本中出现error .so.1.0: cannot open shared object file: No such file or ...
- 由世纪互联运营的 Windows Azure 现已在中国正式发布
我们非常高兴地公开发布由世纪互联运营的 Windows Azure,这标志着我们成为第一家在中国国内正式提供公共云平台技术的跨国公司.这一伟大成就的实现,得益于 Microsoft 与世纪互联的 ...
- http协议与http代理
TCP/IP协议族 TCP/IP(Transmission Control Protocol/InternetProtocol.传输控制协议/网际协议)是用于计算机通信的一个协议族. TCP/IP协议 ...
- C#基础面试
1. 简述Private.Protected.Public.Internal 等访问修饰符的访问权限问题 Private:私有成员,只有类的内部成员可以访问 Protected:保护成员,在类的内部和 ...
- BZOJ 1677: [Usaco2005 Jan]Sumsets 求和( dp )
完全背包.. --------------------------------------------------------------------------------------- #incl ...
- day4作业
作业内容:计算器 #!/usr/bin/env python # -*- coding:utf-8 -*- import re,time #加减字符处理函数 def handle_symbol(cal ...
- 使用python操作RabbitMQ,Redis,Memcache,SQLAlchemy 其二
一.概念 1.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态 ...
- 关于 IE firefox Chrome下的通过用js 关闭窗口的一些问题
首先IE是可以通过window.close()来关闭浏览器窗口的,但是在firefox和Chrome下是无效的,原因在于: ~~~ie可直接<button onclick="windo ...