当前位置: 首页 > news >正文

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

DispatcherServlet概述

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

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

DispatcherServlet初始化

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

  1. 初始化WebApplicationContext
protected WebApplicationContext initWebApplicationContext() {// 获取现有的WebApplicationContextWebApplicationContext rootContext =WebApplicationContextUtils.getWebApplicationContext(getServletContext());WebApplicationContext wac = createWebApplicationContext(rootContext);// 配置WebApplicationContextconfigureAndRefreshWebApplicationContext(wac);return wac;
}
​
  1. 加载配置文件和初始化组件
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);}
}
​
  1. 检查Multipart请求
protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) {return this.multipartResolver.resolveMultipart(request);}return request;
}
​
  1. 获取处理器
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {for (HandlerMapping hm : this.handlerMappings) {HandlerExecutionChain handler = hm.getHandler(request);if (handler != null) {return handler;}}return null;
}
​
  1. 获取处理器适配器
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 + "]");
}
​
  1. 处理视图
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);
}
​
http://www.dtcms.com/a/317200.html

相关文章:

  • PHP-Casbin:现代化 PHP 应用的权限管理引擎
  • 小程序中,给一段富文本字符串文案特殊内容加样式监听点击事件
  • 移动商城平台适配:ZKmall开源商城鸿蒙 / 小程序端开发要点
  • 盲盒抽卡机小程序系统开发:打造个性化娱乐新平台
  • 用html写一个类似于postman可以发送请求
  • 8.6 JavaWeb(请求响应 P67-P74)
  • 生成模型实战 | GPT-2(Generative Pretrained Transformer 2)详解与实现
  • 医疗AI中GPU部署的“非对等全节点架构“方案分析(中)
  • 企业级MCP部署实战:从开发到生产的完整DevOps流程
  • 【C++】unordered系列容器使用及封装
  • 高温环境误检率↓76%!陌讯轻量化检测算法在短袖短裤识别的工业实践
  • Mac 洪泛攻击笔记总结补充
  • Vue2博客项目笔记(完结)
  • 面试问题11
  • 20-C语言:第21~22天笔记
  • V2X通信标准与消息数据结构详解
  • 异构系统数据集成之数据源管理:打通企业数据孤岛的关键一步
  • docker环境搭建
  • Qt Frameless Widget跨平台无边框窗口
  • 基于最大似然估计的卡尔曼滤波与自适应模糊PID控制的单片机实现
  • 鼠标下滑时回跳问题
  • 从“更优”到“更智”:V5.7.3 的交互革新、模式扩展与体验跃迁
  • NodeJs学习日志(1):windows安装使用node.js 安装express,suquelize,sqlite,nodemon
  • ESP32:2.搭建UDP服务器
  • 参考线程池构建一个高性能、配置驱动的Docker容器池
  • Linux---第二天---基础指令
  • copy_file_range系统调用及示例
  • 使用Nginx部署前后端分离项目
  • Docker的安装,服务器与客户端之间的通信
  • Linux基础命令的生产常用命令及其示例简单解释