Rhyme/Struts2源码解析以及拦截器原理模拟

来源:互联网 发布:淘宝手机店铺装修素材 编辑:程序博客网 时间:2024/05/16 02:25

Struts2源码解析以及拦截器原理模拟

这里写图片描述

简单分析已上图就是

1、Struts2的filter过滤请求,调用了Dispatcher的serviceAction()

2、serviceAction()方法创建了ActionProxy,并执行了ActionProxy对象的execute()方法

3、execute()方法又调用了ActionInvocation对象的invoke()方法,ActionInvocation对象中包含所有Intercepters以及对应的Action对象的引用

4、ActionInvocation中的invoke()方法会依次调用容器中的Intercepter,第一个是ExceptionIntercepter

5、ExcepetionIntercepter会调用它的intercepter()方法,执行完相关切面操作之后,会调用ActionInvocation.invoke()方法

6、由于ExcepetionIntercepter调用了ActionInvocation的invoke()方法,所以又会调用下一个Intercepter

7、依次类推,直到所有的Intercepter都被执行一遍

8、然后执行Action中的execute()或其他自定义方法

然后你可以对照struts2的架构图看一下

这里写图片描述

struts2拦截器原理模拟

一共有这么些类

这里写图片描述

下面是模拟代码

1、首先是intercepter接口

这里写图片描述

2、下面是接口的两个实现类FirstIntercepter和SecondIntercepter,注意他们的intercepter方法参数中的ActionInvocation对象的引用

这里写图片描述

这里写图片描述

3、ActionInvocation对象,包含Intercpter的集合还有Action的引用

这里写图片描述

4、Action对象

这里写图片描述

5、ActionProxy 模拟它调用ActionInvocation的invoke方法过程

这里写图片描述

运行结果

这里写图片描述

原创粉丝点击