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

SpringMVC核心处理流程梳理

1、处理流程图展示

当我拿出这张图,阁下又该如何应对呢?执行流程是不是一目了然了。【记住一句话:所有的注解都只是一个标签或者标记,最终都是反射找到具体方法上面的注解标记,然后找到类、属性、方法扩展自己想要的功能功能。】
在这里插入图片描述

2、DispatcherServlet:中央处理器或者中央调度器

在这里插入图片描述

下图官方的解释应该最完善了。
在这里插入图片描述

3、SpringMVC三大核心组件

  • HandlerMapping
    处理器映射器,作用是解析URL,返回执行链以及一些列所需的拦截器(DispatcherServlet源代码1030行的doDispatch(HttpServletRequest request, HttpServletResponse response)方法里面清晰地展示了执行流程,大家有兴趣了可以去看看,就是一个方法套方法的执行链条,最终将请求进行处理,直到返回ModelAndView。)
/*** Process the actual dispatching to the handler.* <p>The handler will be obtained by applying the servlet's HandlerMappings in order.* The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters* to find the first that supports the handler class.* <p>All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers* themselves to decide which methods are acceptable.* @param request current HTTP request* @param response current HTTP response* @throws Exception in case of any kind of processing failure*/@SuppressWarnings("deprecation")// 最核心的请求、响应处理方法protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception //封装好的请求实例HttpServletRequest processedRequest = request;// 处理器执行责任链HandlerExecutionChain mappedHandler = null;//是否是多媒体文件请求boolean multipartRequestParsed = false;//WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);try {ModelAndView mv = null;Exception dispatchException = null;try {processedRequest = checkMultipart(request);multipartRequestParsed = (processedRequest != request);// 传递请求,最终初始化处理器映射器mappedHandler = getHandler(processedRequest);if (mappedHandler == null) {noHandlerFound(processedRequest, response);return;}// 根据前面的处理器映射器初始化处理器适配器HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());// 拿到请求方式String method = request.getMethod();//是否是GET请求boolean isGet = HttpMethod.GET.matches(method);if (isGet || HttpMethod.HEAD.matches(method)) {long lastModified = ha.getLastModified(request, mappedHandler.getHandler());if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {return;}}if (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}// 真正的处理请求,并返回模型和视图mv = ha.handle(processedRequest, response, mappedHandler.getHandler());if (asyncManager.isConcurrentHandlingStarted()) {return;}applyDefaultViewName(processedRequest, mv);mappedHandler.applyPostHandle(processedRequest, response, mv);}catch (Exception ex) {dispatchException = ex;}catch (Throwable err) {// As of 4.3, we're processing Errors thrown from handler methods as well,// making them available for @ExceptionHandler methods and other scenarios.dispatchException = new NestedServletException("Handler dispatch failed", err);}// 封装视图结果processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);}catch (Exception ex) {triggerAfterCompletion(processedRequest, response, mappedHandler, ex);}catch (Throwable err) {triggerAfterCompletion(processedRequest, response, mappedHandler,new NestedServletException("Handler processing failed", err));}finally {if (asyncManager.isConcurrentHandlingStarted()) {// Instead of postHandle and afterCompletionif (mappedHandler != null) {mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);}}else {// Clean up any resources used by a multipart request.if (multipartRequestParsed) {cleanupMultipart(processedRequest);}}}}
http://www.lryc.cn/news/269333.html

相关文章:

  • go 语言程序设计第2章--程序结构
  • JavaScript基础知识点总结:从零开始学习JavaScript(五)
  • Intel FPGA 技术开放日
  • 分享72个Python爬虫源码总有一个是你想要的
  • Mybatis 动态 SQL - foreach
  • 编程笔记 GOLANG基础 001 为什么要学习Go语言
  • OrientDB使用教程:全面了解图数据库
  • VMware安装笔记
  • MIT线性代数笔记-第27讲-复数矩阵,快速傅里叶变换
  • 三维点通用排序
  • [架构之路-265]:目标系统 - 设计方法 - 软件工程 - 软件设计 - 如何做好详细设计
  • java设计模式学习之【模板方法模式】
  • 篇章二 | Python 入门指南:深入理解基础数据类型
  • 循环冗余效验码的计算方法
  • 第P8周:YOLOv5-C3模块实现
  • Java中常见的日志包分析(Log4j、Logback、SLF4J等)
  • C++系列-第1章顺序结构-3-输出类cout
  • 对于智能设备的一些设想1
  • Large-Precision Sign using PBS
  • 【电商项目实战】MD5登录加密及JSR303自定义注解
  • 2014,TEVC,A competitive swarm optimizer for large scale optimization(CSO)
  • 【机器学习】【线性回归】梯度下降
  • JMeter逻辑控制器之While控制器
  • 记录 Docker 外部访问的基本操作
  • 【Android 13】使用Android Studio调试系统应用之Settings移植(六):BannerMessagePreference
  • Python 变量
  • ComfyUI如何中文汉化
  • Glary Utilities Pro - 电脑系统优化全面指南:详尽使用教程
  • 1.4分页和排序
  • Modbus转Profinet,不会编程也能用!轻松快上手!