SoFunction
Updated on 2025-04-11

A detailed explanation of the implementation principle of Spring intercept chain

In web application development, an interceptor is a very important mechanism that can perform pre- and post-processing at all stages of request processing. The Spring framework provides a powerful and flexible interceptor mechanism that enables developers to easily insert custom logic into request processing flows.

Understanding the implementation principle of Spring intercept chains will not only help us better use the functions provided by Spring, but also allow us to customize complex request processing logic when needed.

1. What is an interceptor chain?

Simply put, an interceptor chain is a list of interceptors that process requests, intercepting and processing requests one by one in a certain order. Each interceptor can execute some logic before, after, or after the request is processed.

For example, we may need to perform permission verification before all requests are processed, log the log after processing, or release the resource after the request is completed. These operations can be achieved by defining different interceptors, each responsible for a specific task.

2. Intercept chain in Spring

In Spring MVC, the interceptor chain is throughHandlerInterceptorInterfaces and implementation classes are implemented. Spring'sDispatcherServletAs a front controller, it is responsible for coordinating various stages of requests, including calling interceptors.

The implementation of the interceptor chain allows multiple interceptors to process requests in a certain order. Each interceptor has the opportunity to execute specific logic before and after request processing, which provides great flexibility for us to insert custom logic into the request processing flow.

3. The core components of the intercept chain

To understand the implementation principle of intercepting chains, you first need to understand the role and relationship between several core components in Spring MVC:

HandlerMappingHandlerMappingResponsible for mapping the request URL to a specific processor (Handler). A processor is usually a controller method. Spring offers a variety ofHandlerMappingImplementation, such asRequestMappingHandlerMapping, supports annotation-based mapping.

HandlerAdapterHandlerAdapterIt is the component responsible for executing a specific processor. It knows how to call a specific type of processor and returns aModelAndViewObject, used to render views.

DispatcherServletDispatcherServletIt is the core component of Spring MVC and serves as the current controller. It receives all HTTP requests and coordinatesHandlerMappingHandlerAdapterComponents such as view resolution will eventually distribute the request to the appropriate processor for processing.

HandlerInterceptorHandlerInterceptorThe interface defines the basic behavior of the interceptor. By implementing this interface, custom logic can be inserted at different stages of request processing, such as before, after request, or after completion processing.

4. The workflow of intercepting chains

After understanding the core components, let's look at how interceptor chains collaborate among these components.

  • Request to arrive at DispatcherServlet: All HTTP requests are first made byDispatcherServlettake over.
  • Find HandlerDispatcherServletuseHandlerMappingFind a processor (Handler) that matches the request URL.
  • Apply interceptor front: Before calling the processor,DispatcherServletWill call all registered interceptorspreHandlemethod. These interceptors are executed in sequence in the defined order. If any interceptorpreHandlereturnfalse, the request will be terminated and subsequent interceptors and processors will not execute.
  • Call HandlerAdapter to execute Handler: All pre-interceptorspreHandleMethod returntrueback,DispatcherServletWill callHandlerAdapterExecute specific processor methods (such as those in Controller).
  • Apply interceptor rear: After the processor is executed,DispatcherServletWill call the interceptorpostHandleMethods, these interceptors are executed in reverse order in the defined order.
  • Rendering viewDispatcherServletUse a view resolver (ViewResolver) to render the final view, such as returning an HTML page.
  • Complete the interceptor:at last,DispatcherServletCalling the interceptorafterCompletionMethod, notify the interceptor request has been completed and the same is executed in reverse order.

This process ensures that the interceptor can insert logic at different stages of request processing, such as verification, logging, performance monitoring, etc.

5. Source code analysis

To deeply understand the implementation principle of intercept chains, we need to check the source code of Spring MVC. Below, we will analyze it step by stepDispatcherServletHandlerMappingHandlerAdapterandHandlerInterceptorThe source code of components such as the implementation of the comprehensive understanding of the intercept chain.

5.1 The role of DispatcherServlet

DispatcherServletIt is the central hub of the entire Spring MVC request processing process. It undertakes tasks such as receiving requests, finding processors, executing interceptors, calling processors, rendering views, etc.

Let's seeDispatcherServletKey source code related to intercept chains.

public class DispatcherServlet extends FrameworkServlet {
    // ...Other codes...
    @Override
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // ...Initialization context...
        // 1. Find the processor        HandlerExecutionChain mappedHandler = getHandler(processedRequest);
        if (mappedHandler == null) {
            noHandlerFound(request, response);
            return;
        }

        HttpServletRequest webRequest = createWebRequest(request, response);
        try {
            // 2. Apply the preHandle method of the interceptor            HandlerInterceptor[] interceptors = ();
            if (!applyPreHandle(processedRequest, response, mappedHandler, interceptors)) {
                return;
            }

            // 3. Call the processor            ModelAndView mv = (processedRequest, response, ());

            // 4. Apply the postHandle method of the interceptor            applyPostHandle(processedRequest, response, mappedHandler, mv, interceptors);

            // 5. Render view            render(mv, request, response);

            // 6. Apply the afterCompletion method of the interceptor            triggerAfterCompletion(processedRequest, response, mappedHandler, null, interceptors);
        } catch (Exception ex) {
            // Exception handling            // ...
        }
    }

    // ...Other codes...}

As can be seen from the above code,DispatcherServletDuring the process of processing the request, the search processor and the application interceptor are executed in turn.preHandle, calling processors, applying interceptorspostHandle, rendering views and applying interceptorsafterCompletionSteps of the method.

5.2 HandlerMapping search mechanism

HandlerMappingUsed to map requests to specific processors. Spring MVC provides a variety ofHandlerMappingImplementation, such as annotation-basedRequestMappingHandlerMapping

Let's see a simple onegetHandlerMethod implementation:

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    return (request);
}

here,handlerMappingsIt's oneList<HandlerMapping>, Spring will traverse these in orderHandlerMapping, until a matching processor is found.

RequestMappingHandlerMappingA core approach isgetHandlerInternal

@Override
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
    // Find matching HandlerMethod based on request URL and HTTP methods    // Return a HandlerMethod object, containing controller instance and method information}

5.3 HandlerAdapter adaptation logic

HandlerAdapterResponsible for calling the specific processor. Spring MVC provides a variety ofHandlerAdapterImplementation, such asRequestMappingHandlerAdapter

have a lookgetHandlerAdapterandhandlemethod:

protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    return (request, response, handler);
}

handlerAdapterIt's oneHandlerAdapterThe implementation of the interface is throughsupportsThe method determines whether it is adapted and then callshandleMethod executes the processor.

RequestMappingHandlerAdapterofhandleMethod example:

@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // Call Controller method to process the request    // Bind request parameters, perform data binding and verification    // Return the ModelAndView object}

5.4 HandlerInterceptor execution process

existDispatcherServletofdoDispatchIn the method, we see the call flow of the interceptor. Let's take a closer look at how these interceptors are executed.

Suppose we have oneHandlerExecutionChainObject, which contains the processor and a set of interceptors:

public class HandlerExecutionChain {
    private final Object handler;
    private final List<HandlerInterceptor> interceptors;
    
    // Getters and constructors
}

existapplyPreHandleIn the method,DispatcherServletEach interceptor will be called in sequencepreHandlemethod:

protected boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response, HandlerExecutionChain mappedHandler)
        throws Exception {

    HandlerInterceptor[] interceptors = ();
    if(!(interceptors)) {
        for (HandlerInterceptor interceptor : interceptors) {
            if (!(request, response, ())) {
                triggerAfterCompletion(request, response, mappedHandler, null, interceptors);
                return false;
            }
        }
    }
    return true;
}

Similarly, inapplyPostHandleandtriggerAfterCompletionIn the method, the interceptorpostHandleandafterCompletionThe methods are called in sequence, in orderpreHandleon the contrary.

6. Implementation of custom interceptors

After understanding the basics of intercepting chains, let's take a look at how to customize interceptors in Spring. Here is a simple custom interceptor example:

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // Execute before request processing        ("Pre Handle method is Calling");
        return true; // Return true to continue the process, return false to interrupt    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        // After the request is processed, but before the view is rendered        ("Post Handle method is Calling");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // Execute after rendering the view        ("Request and Response is completed");
    }
}

Registering this interceptor can be configured through Java or XML. In Spring Boot, it can be implemented byWebMvcConfigurerInterface configuration:

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        (new MyInterceptor())
                .addPathPatterns("/**") // Intercept all requests                .excludePathPatterns("/login"); // Exclude specific paths    }
}

With such a configuration,MyInterceptorIt will intercept all requests except/loginpath.

6.1 The order of execution of interceptors

If multiple interceptors are defined, Spring will be executed in the order of registrationpreHandlemethod, and executepostHandleandafterCompletionIt is in reverse order. For example:

@Override
public void addInterceptors(InterceptorRegistry registry) {
    (new FirstInterceptor());
    (new SecondInterceptor());
}

Execution order:

This design allows post-registered interceptors to perform post-logic and completion logic first.

7. Summary

In this article, we have discussed in-depth the implementation principle and source code analysis of Spring intercept chains, from core components such asDispatcherServletHandlerMappingHandlerAdapterandHandlerInterceptorfunctions, to the workflow of the intercept chain, to actual source code analysis and custom interceptor implementation.

Understanding these principles not only helps us better use the interceptor functions provided by Spring, but also allows us to customize complex interception logic when needed, enhancing application flexibility and maintainability.

This is the end of this article about a detailed explanation of the implementation principle of Spring intercept chain. For more related contents of Spring intercept chains, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!