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

深入理解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.xdnf.cn/news/17125.html

相关文章:

  • Flink CDC如何保障数据的一致性?
  • 亚矩阵云手机:解锁 Shopee/Lazada 东南亚电商运营“通关密码
  • WordPress自定义.js文件排序实现方法
  • Unity里的对象旋转数值跳转问题的原理与解决方案
  • Spring Boot集成方案 + Elasticsearch向量检索,语义搜索核弹
  • Linux seLinux
  • AI大语言模型如何重塑软件开发与测试流程
  • 3D开发引擎HOOPS赋能AEC领域:可视化技术助力建筑数字化转型!
  • Promise
  • 【JS-7-ajax】AJAX技术:现代Web开发的异步通信核心
  • Python包管理新利器:uv全面解析与Conda对比指南
  • 一文读懂:什么是CLIP
  • Redis集群核心原理与实战解析
  • C语言的数组与字符串练习题2
  • 【前端开发】四. JS内置函数
  • 5G毫米波射频前端测试:OTA暗室与波束成形性能验证
  • RuoYi-Cloud 微服务本地部署详细流程实录(IDEA + 本地 Windows 环境)
  • 商派小程序商城(小程序/官网/APP···)的范式跃迁与增长再想象
  • 参考线程池构建一个高性能、配置驱动的Docker容器池
  • 基于Simulink/MWORKS的文字与开关量混合传输系统设计
  • [LVGL] 部件lv_obj | 样式lv_style | LV_PART_ | LV_STATE_
  • Cartographer安装测试与模块开发(四)--Cartographer纯定位思路
  • Linux基本指令:掌握系统操作的钥匙
  • 浅谈RNN被Transformer 取代的必然性
  • 面试题:基础的sql命令
  • 在LLM小型化趋势下,AI Infra需要做出哪些相应调整?
  • 【完整源码+数据集+部署教程】爬行动物异常检测系统源码和数据集:改进yolo11-GhostDynamicConv
  • JavaScript 中 var、let 和 const 的区别与使用场景
  • TCP的三次握手和四次挥手实现过程。以及为什么需要三次握手?四次挥手?
  • [GESP202309 四级] 2023年9月GESP C++四级上机题题解,附带讲解视频!