为啥拦截器在invocation.invoke()之前的代码,将会在Action之前被依次执行,而在invocation.invoke ()之后的代码,将会在Action之后被逆序执行?

只是小知识点,有点意思,记下来

因为 可以简化成下面两段代码:

----------------------------------
    before(invocation);  
 
        // 调用下一个拦截器,如果拦截器不存在,则执行Action  
 
    result = invocation.invoke();  
 
    after(invocation, result);

----------------------------------
interceptor.getInterceptor().intercept(DefaultActionInvocation.this);  

----------------------------------
相当于递归前后加执行语句,相当于一层层的执行,加上很多的分支,类似下面的结构
所以before是从外到内,after是从内到外,一一对应
before();
        {
            before();
            {
                before();
                {
                    before();
                    {
                        //invocation.invoke();
                    }
                    after();
                }
                after();
            }
            after();
        }
        after();

利用到了递归中增加层次,从而实现堆+栈效果,有点类似多层ifelse


猜你喜欢

转载自blog.csdn.net/xiaoe3504xiaoe/article/details/53198697