深入理解SpringMVC DispatcherServlet源码及全流程原理

DispatcherServlet概述

DispatcherServlet是SpringMVC的前端控制器(Front Controller),它负责拦截所有进入应用的HTTP请求,分发请求给相应的处理器,并处理返回的响应。DispatcherServlet的主要职责包括:

  • 初始化WebApplicationContext
  • 加载各种组件(如HandlerMapping、HandlerAdapter等)
  • 分发请求
  • 视图解析和渲染

DispatcherServlet初始化

DispatcherServlet的初始化过程包括创建和配置Spring的WebApplicationContext以及加载各种必要的组件。下面是 DispatcherServlet初始化的主要步骤:

  1. 初始化WebApplicationContext

    protected WebApplicationContext initWebApplicationContext() {
    // 获取现有的WebApplicationContext
    WebApplicationContext rootContext =
    WebApplicationContextUtils.getWebApplicationContext(getServletContext());

    复制代码
     WebApplicationContext wac = createWebApplicationContext(rootContext);
     // 配置WebApplicationContext
     configureAndRefreshWebApplicationContext(wac);
     return wac;

    }

  2. 加载配置文件和初始化组件

    protected void initStrategies(ApplicationContext context) {
    initMultipartResolver(context);
    initLocaleResolver(context);
    initThemeResolver(context);
    initHandlerMappings(context);
    initHandlerAdapters(context);
    initHandlerExceptionResolvers(context);
    initRequestToViewNameTranslator(context);
    initViewResolvers(context);
    initFlashMapManager(context);
    }

请求处理流程

当一个HTTP请求到达时,DispatcherServlet将按以下步骤处理请求:

  1. 请求接收

    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HttpServletRequest processedRequest = request;
    HandlerExecutionChain mappedHandler = null;
    ModelAndView mv = null;
    Exception dispatchException = null;

    复制代码
     try {
         // 检查并包装Multipart请求
         processedRequest = checkMultipart(request);
    
         // 获得处理器
         mappedHandler = getHandler(processedRequest);
         if (mappedHandler == null) {
             noHandlerFound(processedRequest, response);
             return;
         }
    
         // 获得处理器适配器
         HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
    
         // 执行处理器
         mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
    
         // 处理视图
         processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
     }
     catch (Exception ex) {
         dispatchException = ex;
         processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
     }

    }

  2. 检查Multipart请求

    protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
    if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) {
    return this.multipartResolver.resolveMultipart(request);
    }
    return request;
    }

  3. 获取处理器

    protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    for (HandlerMapping hm : this.handlerMappings) {
    HandlerExecutionChain handler = hm.getHandler(request);
    if (handler != null) {
    return handler;
    }
    }
    return null;
    }

  4. 获取处理器适配器

    protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
    for (HandlerAdapter ha : this.handlerAdapters) {
    if (ha.supports(handler)) {
    return ha;
    }
    }
    throw new ServletException("No adapter for handler [" + handler + "]");
    }

  5. 处理视图

    protected void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
    HandlerExecutionChain mappedHandler, ModelAndView mv, Exception exception) throws Exception {
    if (exception != null) {
    if (mv == null) {
    throw exception;
    }
    mv.addObject("exception", exception);
    }

    复制代码
     if (mv != null) {
         render(mv, request, response);
     }

    }

处理器映射

处理器映射(Handler Mapping)负责将请求URL映射到相应的处理器。常用的处理器映射有:

  • RequestMappingHandlerMapping:基于注解的处理器映射。

  • BeanNameUrlHandlerMapping:基于Bean名称的处理器映射。

    public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
    HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
    return handlerMethod != null ? new HandlerExecutionChain(handlerMethod) : null;
    }

处理器适配

处理器适配(Handler Adapter)负责调用处理器方法。常用的处理器适配有:

  • RequestMappingHandlerAdapter:适配使用@RequestMapping注解的方法。

  • HttpRequestHandlerAdapter:适配实现HttpRequestHandler接口的处理器。

    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    return ((HandlerMethod) handler).invokeForRequest(request, response);
    }

视图解析

视图解析(View Resolver)负责将逻辑视图名称解析为实际视图对象,并渲染视图。常用的视图解析器有:

  • InternalResourceViewResolver:解析JSP视图。

  • ThymeleafViewResolver:解析Thymeleaf视图。

    public View resolveViewName(String viewName, Locale locale) throws Exception {
    return getView(viewName, locale);
    }

    protected View getView(String viewName, Locale locale) throws Exception {
    return (viewName != null ? new InternalResourceView(viewName) : null);
    }

相关推荐
ZC跨境爬虫26 分钟前
海南大学交友平台开发实战 day10(后端向前端输出_前端读取数据全流程联调+日志调试落地)
前端·python·sqlite·html·状态模式
前端不太难5 小时前
OpenClaw 的设备架构设计:如何在有限算力上跑复杂智能体
状态模式·openclaw
Rabbit_QL1 天前
【服务出错问题排查记录】从一个“点击失败”开始:为什么“系统异常”其实是最差的错误设计
状态模式
老神在在0013 天前
Spring Boot 全局异常处理器(GlobalExceptionHandler)
spring boot·spring·java-ee·状态模式·
小陈工3 天前
Python Web开发入门(十八):跨域问题解决方案——从“为什么我的请求被拦了“到“我让浏览器乖乖听话“
开发语言·python·机器学习·架构·数据挖掘·回归·状态模式
前端不太难3 天前
鸿蒙 PC 的机会在哪里?
华为·状态模式·harmonyos
老神在在0013 天前
企业级 SpringBoot 后端通用开发规范|统一响应 + 敏感字段加密
spring boot·后端·状态模式
liweiweili1264 天前
http数据传输过程数据编码解码问答
网络协议·http·状态模式
薛定谔的悦4 天前
IEC 60870-5-104协议解析——电力系统远动通信实战
linux·状态模式·储能·ems
前端不太难4 天前
鸿蒙 App、PC、游戏,本质是同一套系统吗?
游戏·状态模式·harmonyos