Explanation of invocation.invoke() in the interceptor

  1. public abstract class AroundInterceptor extends AbstractInterceptor {  
  2.       
  3.     /* (non-Javadoc) 
  4.      * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation) 
  5.      */  
  6.     @Override  
  7.     public String intercept(ActionInvocation invocation) throws Exception {  
  8.         String result = null;  
  9.   
  10.         before(invocation);  
  11.         // Call the next interceptor, if the interceptor does not exist, execute the Action  
  12.         result = invocation.invoke();  
  13.         after(invocation, result);  
  14.   
  15.         return result;  
  16.     }  
  17.       
  18.     public abstract void before(ActionInvocation invocation) throws Exception;  
  19.   
  20.     public abstract void after(ActionInvocation invocation, String resultCode) throws Exception;  
  21.   
  22. }  

 

 

What I need to point out here is a very important method invocation.invoke(). This is the method in ActionInvocation, and ActionInvocation is the Action dispatcher, so this method has the following two meanings: 

1. If there are other Interceptors in the interceptor stack, then invocation.invoke() will call the next Interceptor in the stack. implement. 2. If there is only Action in the interceptor stack, then invocation.invoke() will call Action to execute. Therefore, we can find that the method invocation.invoke() is actually the core of the implementation of the entire interceptor framework. Based on this implementation mechanism, we can also get the following two very important inferences:  1. If in the interceptor, we do not use invocation.invoke() to complete the call to the next element in the stack, but directly return a character string as the result of execution, then the entire execution will be aborted. 2. We can divide the code in the interceptor into two parts with invocation.invoke() as the boundary. The code before invocation.invoke() will be executed in sequence before Action, and the code before invocation.invoke() will be executed in sequence. The following code will be executed in reverse order after the Action. From this, we can use invocation.invoke() as the real interception point of the Action code, thus realizing AOP.  

 



 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326596001&siteId=291194637