首页 > 其他分享 >Struts2 中的Aop AOP思想

Struts2 中的Aop AOP思想

时间:2023-03-01 12:33:09浏览次数:34  
标签:拦截器 return String invoke AOP struts2 Struts2 Aop public


认识aop还是从了解struts2中的拦截器了解这个思想的,struts2中的拦截可插拔式的配置方式刚开始让我半懂不懂的,只知道按着自己的需要配置就行了。

 前一段时间翻看struts2中的源代码,发现原来struts2中的aop也不过如此。

众所周知,struts2的拦截器是来自于Interceptor接口,或者继承了抽象类AbstractInterceptor。

观看所有的拦截器,实现的intercept(ActionInvocation invocation)方法。

ActionInvocation  又称上Struts2的下文,里面包含了请求的参数和请求携带的不少系统参数。

拦截器验证通过以后执行这行代码。

return invocation.invoke();

每个拦截器都在执行这行代码。我翻了下ActionInvocation的实现类,有两个,分别是

DefaultActionInvocation和MockActionInvocation

翻看MockActionInvocation中的代码,没有找到invoke()方法,

在DefaultActionInvocation实现类中找到invoke()和一个相关的参数。

protected Iterator interceptors;//解析struts2的配置文件的时候将所有的拦截器都放到了这里

public String invoke() throws Exception {
String profileKey = "invoke: ";
try {
UtilTimerStack.push(profileKey);

if (executed) {
throw new IllegalStateException("Action has already executed");
}

if (interceptors.hasNext()) {//这里迭代了所有的interceptor
final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
String interceptorMsg = "interceptor: " + interceptor.getName();
UtilTimerStack.push(interceptorMsg);
try {
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
}
finally {
UtilTimerStack.pop(interceptorMsg);
}
} else {
resultCode = invokeActionOnly();
}

// this is needed because the result will be executed, then control will return to the Interceptor, which will
// return above and flow through again
if (!executed) {
if (preResultListeners != null) {
for (Object preResultListener : preResultListeners) {
PreResultListener listener = (PreResultListener) preResultListener;

String _profileKey = "preResultListener: ";
try {
UtilTimerStack.push(_profileKey);
listener.beforeResult(this, resultCode);
}
finally {
UtilTimerStack.pop(_profileKey);
}
}
}

// now execute the result, if we're supposed to
if (proxy.getExecuteResult()) {
executeResult();
}

executed = true;
}

return resultCode;
}
finally {
UtilTimerStack.pop(profileKey);
}
}

再结合下面的代码

public String intercept(ActionInvocation invocation) throws Exception {
return invocation.invoke();//原来这里相当于一个递归方法了,一下子恍然大悟。
}

看到这里,我们就不难的把struts2中的aop的思想抽出来使用

/**核心接口*/
public interface baseA{
public String invoke() throws Exception;
}
public class BaseB implements baseA{
protected Iterator interceptors;//所有可插拔的参数
public String invoke() throws Exception{
String returnStr=null;
if (interceptors.hasNext()) {
returnStr="执行所拦截,或者说可配置的方法()";//在外面包装都执行完了,才执行最后的目标方法
}else{
returnStr="执行目标最后要执行的方法()";
}
return returnStr;
}
}
//既然你想实现可插拔的效果,那你必须遵循我的定义的规范,也就是Interceptor了
public interface Interceptor extends Serializable {
public String intercept(baseA basea) throws Exception;//这个是我定义的规范,你必须实现他,才可以使用我的可插拔的方式,而且你实现我这个接口的时候最后验证过了必须执行 basea.invoke();这个方法
}

好吧,到此struts2中的aop已经抽出来了,想想aop的思想也不过这样。可怜自己学的时候可是死记硬背下

标签:拦截器,return,String,invoke,AOP,struts2,Struts2,Aop,public
From: https://blog.51cto.com/yxkong/6093425

相关文章

  • 理论:第二章:Spring的AOP和IOC是什么?使用场景有哪些?Spring事务与数据库事务,传播行为,数据
    AOP:面向切面编程。即在一个功能模块中新增其他功能,比方说你要下楼取个快递,你同事对你说帮我也取一下呗,你就顺道取了。在工作中如果系统中有些包和类中没有使用AOP,例如日志......
  • Spring配置切面(AOP)
    AOP,面向接口的编程,实际上是代理模式的实现。参考:代理模式(Proxy)   一、使用Scheme-based方式配置需要实现接口重写指定方法,来确定通知所在位置。(一)、前置通知和......
  • SpringBoot全局异常封装:AOP增强
    api请求错误返回json,页面请求错误跳转报错页面:自动装配、异常通知两个方法Java异常类错误无法避免,通常由于系统原因造成。如IOError,注意不是IOException,原因可能是......
  • Spring不同版本的AOP
    1、Spring4、SpringBoot11.1代码实现publicinterfaceCalculator{intdiv(inta,intb);}@ComponentpublicclassCalculatorImplimplementsCalcul......
  • 基于注解的AOP
    1、引入依赖<!--springaop依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><ver......
  • spring aop切面说明
    execution:处理JoinPoint的类型,例如call、execution(*android.app.Activity.on**(..)):这个是最重要的表达式,第一个*表示返回值,*表示返回值为任意类型,后面这个就是典型的......
  • nexus Snaopshpt 包迁移后引起的打包依赖问题
    问题描述1:在nexus迁移后,maven的setting.xml引用新nexus地址,之前的nexus地址做为repository,由于新nexus中有迁移的包,所以会先去新nexus中找包去使用,因为有相同的包此时老n......
  • AOP
    AOP面向切面编程面向对象的编程思想是从上往下,但是面向切面编程的时候就是横向的,思考如下:创建出对象,里面有a对象的方法,也有b对象的方法,横向抽取两个对象的方法,然后存如......
  • 前端ORA-03113及后台ORA-07445[evaopn3()+135报错的处理
    遇到前端业务SQL执行报错ORA-03113,后台ORA-07445[evaopn3()+135报错;经与MOS上文档的分析对比,ExecutingaQueryWithPeoplesoft,LeadstoORA-07445:exceptionencounter......
  • 【Spring AOP】【十】Spring AOP源码解析-讲一下ExposeInvocationInterceptor
    1 前言不知道你在调试的时候,有没有发现我们的通知器链上首个元素会给我放进来一个ExposeInvocationInterceptor类型的通知器,看下图是不是,我们在之前其实也说过一次只是......