SpringMVC---Interceptor

Interceptor configuration:

  • Interceptors in SpringMVC are used to intercept the execution of controller methods
  • Interceptors in SpringMVC need to implement HandlerInterceptor or inherit HandlerInterceptorAdapter class
  • Spring's interceptor must be configured in the SpringMVC configuration file:

image.pngLet's analyze the execution process of the interceptor through the source code:

image.pngWhat are these three methods for?

image.png

image.pngThe handle here is actually our controller controller

image.pngLet's go in and look at this processDispatchResult():

image.png

image.png

So we know: the three important methods of the interceptor are before the controller is executed, after the controller is executed, and after the view is rendered.

Create interceptor:

image.png

Our custom interceptor implements the HandlerInterceptor interface and implements three methods:

  • preHandle()
  • postHandle()
  • afterCompletion()

Observe the source code: when preHandle() returns true, the execution order of each method of the interceptor:

Three abstract methods of interceptors: Interceptors in SpringMVC have three abstract methods

  • preHandle: Execute preHandle() before the execution of the controller method. Its return value of boolean type indicates whether to intercept or release, return true to release, that is, call the controller method, if it is false, it means intercept, and the controller method will not be called
  • postHandle: Execute postHandle() after the controller method is executed
  • afterCompletion: After processing the view and model data, execute afterCompletion() after rendering the view

The execution order of multiple interceptors:
i. If the preHandle() of each interceptor returns true,
the execution order of multiple interceptors is related to the configuration order of the interceptors in the SpringMVC configuration file:
preHandle() will follow the configuration Executed in the order of postHandle() and afterComplation() will be executed in the reverse order of configuration

ii. If the preHandle() of an interceptor returns false,
preHandle() returns false and the preHandle() of the interceptor before it will execute , postHandle() is not executed, and afterComplation() of the interceptor before the interceptor that returns false will execute

image.pngCheck the console:

image.pngIt is true that the preHandle method is executed in order, while postHandle() and afterCompletion() are executed in reverse order.
DeBUG view source code:

image.pngTherefore, this mappedHandler is an execution chain, which contains the interceptor of our control method and processor method.

image.pngHere this.interceptorIndex is the index value of the previous interceptor of the current interceptor

image.png

image.png

image.png

image.png

None of postHandle will be executed, because afterCommletion is executed according to the index, so it will return the interceptor before the interceptor is false, and preHandle will execute the interceptor before false and the interceptor before it. (If you don't execute the interceptor that returns false, then you don't know if the interceptor returns false)

The above is the source code of some core execution sequences of the interceptor, I hope it can help everyone.

Guess you like

Origin juejin.im/post/7219738264851710010