Spring learning -- interceptor of Spring MVC

Before learning the development of interceptors, we need to make clear the difference between filters and interceptors, and find a blog that speaks clearly. You can refer to it. Difference between interceptor and filter

Article catalog

1, Development interceptor class

In order to better illustrate the execution order of interceptors, I have written two interceptor classes with the same content. Here I just paste the code of one of them.

package cool.gjh.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Interceptor
 * <p>
 * Interception processing for / student request
 * </p>
 *
 * @author ACE_GJH
 */
@Component
public class StudentInterceptor1 implements HandlerInterceptor {
    /**
     * Request the first pass method before calling HandlerAdapter.
     *
     * @param request  current HTTP request
     * @param response current HTTP response
     * @param handler  chosen handler to execute, for type and/or instance evaluation
     * @return {@code true} Release request; {@ code false} interrupt request
     * @throws Exception in case of errors
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("StudentInterceptor1:preHandle");
        return true;
    }

    /**
     * The request is called after HandlerAdapter.
     *
     * @param request      current HTTP request
     * @param response     current HTTP response
     * @param handler      handler (or {@link HandlerMethod}) that started asynchronous
     *                     execution, for type and/or instance examination
     * @param modelAndView the {@code ModelAndView} that the handler returned
     *                     (can also be {@code null})
     * @throws Exception in case of errors
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("StudentInterceptor1:postHandle");
    }

    /**
     * After the request is completed, it is called.
     * <p>Note: only called if the {@ code preHandle} method of this interceptor completes successfully and returns {@ code true}!
     * <p>Like the {@ code postHandle} method, this method will be called in reverse order on each interceptor in the chain, so the first interceptor will be the last one to be called.
     *
     * @param request  current HTTP request
     * @param response current HTTP response
     * @param handler  handler (or {@link HandlerMethod}) that started asynchronous
     *                 execution, for type and/or instance examination
     * @param ex       exception thrown on handler execution, if any
     * @throws Exception in case of errors
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("StudentInterceptor1:afterCompletion");
    }
}

2, Configure interceptors

stay springmvc.xml Configuration in:

  <!-- Configuring Interceptors  -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/student/*"/>
            <ref bean="studentInterceptor1"/>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/student/*"/>
            <ref bean="studentInterceptor2"/>
        </mvc:interceptor>
    </mvc:interceptors>

3, Test effect

visit: http://localhost:8080/spring/student/data

Conclusion: the timing and order of execution of preHandle(), postHandle(), and afterCompletion() in the interceptor can be seen clearly by printing the results.

4, Execution sequence of interceptors

In the above conclusion, we can see the execution timing of the three methods in the same interceptor. If there are multiple interceptors in the project, and there are sequential requirements for intercepting a certain request path, how to configure them? I'm sure you've already thought about it. Follow the configuration file< mvc:interceptor >The order of the labels is related. Yes, let's try it.
springmvc.xml Adjust the configuration order of interceptors:

<!-- Configuring Interceptors  -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/student/*"/>
            <ref bean="studentInterceptor2"/>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/student/*"/>
            <ref bean="studentInterceptor1"/>
        </mvc:interceptor>
    </mvc:interceptors>

Test results:

Conclusion: the execution order and configuration order of interceptor configuration are consistent.

Tags: xml Spring

Posted on Thu, 18 Jun 2020 02:49:34 -0400 by kituscat