Backend Development 10 min read

Applying the Chain of Responsibility Pattern in Spring MVC Interceptors

This article explains how the Chain of Responsibility design pattern is implemented within Spring MVC interceptors, detailing the request flow, interceptor lifecycle methods, and practical differences from the classic GoF definition, while also exploring other typical use cases of the pattern.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Applying the Chain of Responsibility Pattern in Spring MVC Interceptors

In this article the author discusses the application of the 责任链 (Chain of Responsibility) design pattern in Spring MVC interceptors, without delving into the full mechanics of Spring MVC interceptors themselves. Readers are referred to a related post for deeper details.

前言

The essence of the Chain of Responsibility pattern is to decouple request senders from receivers by arranging multiple objects in a 链式 structure, where each object (handler) gets a chance to process the request until one handles it.

In simpler terms, the pattern allows configuring multiple 处理器 that are invoked sequentially for the same request.

When a request arrives, the configured 处理器 are traversed in order (e.g., processor A decides whether to handle the request; if it does, processing stops, otherwise it proceeds to the next processor), resembling a linked‑list data structure.

Because the pattern resembles a linked list, each processor maintains a reference to the next one, mirroring the classic definition of the responsibility chain.

SpringMVC 中的拦截器

The diagram shows the overall workflow of Spring MVC interceptors.

请求进入拦截器链 : An incoming HTTP request first passes through DispatcherServlet , whose doDispatch method selects the appropriate controller based on URL and handler mapping.

拦截器执行 : After the handler is found, the interceptor chain runs. Each interceptor can execute custom logic at different stages via preHandle , postHandle , and afterCompletion methods.

响应返回给客户端 : Finally, DispatcherServlet returns the view result to the client.

Spring MVC’s interceptor mechanism is a concrete application of the Chain of Responsibility pattern. The framework stores configured interceptors in a HandlerExecutionChain and invokes them before and after the controller execution.

HandlerExecutionChain # applyPreHandle and applyPostHandle method signatures
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // <1> Get interceptor array
    HandlerInterceptor[] interceptors = getInterceptors();
    if (!ObjectUtils.isEmpty(interceptors)) {
        // <2> Iterate over interceptors
        for (int i = 0; i < interceptors.length; i++) {
            // Determine if can handle and process
        }
    }
    // <4> Return true, pre‑handle succeeded
    return true;
}

void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @Nullable ModelAndView mv) throws Exception {
    HandlerInterceptor[] interceptors = getInterceptors();
    if (!ObjectUtils.isEmpty(interceptors)) {
        // Reverse iterate interceptors, omitted logic
    }
}

Within Spring MVC, DispatcherServlet calls doDispatch() , which invokes applyPreHandle() and applyPostHandle() on the HandlerExecutionChain to process the request.

Although Spring MVC’s interceptor chain does not follow the strict GoF definition—where processing stops as soon as a handler handles the request—it still embodies the spirit of the pattern, allowing multiple interceptors to act on a request for concerns such as authentication, logging, etc.

责任链的其他应用场景

Beyond interceptors, the Chain of Responsibility can be used for logging (routing logs to console, file, database), authentication/authorization pipelines, and e‑commerce order processing steps.

总结

While the GoF definition emphasizes early termination, practical implementations like Spring MVC interceptors often let the request pass through all configured handlers, adapting the pattern to real‑world needs.

If this article helped you, feel free to follow, like, and bookmark.

chain of responsibilityJavabackend developmentInterceptordesign patternSpringMVC
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.