原理
  
  本质是将DispatcherServlet及关联的Spring上下文环境的初始化工作织入Servlet的生命周期内,将外部WEB请求转换为Spring Bean能处理的形式,然后将处理后的结果借助于符合J2EE规范组件,呈现给客户端。
  
  步骤
  
  HttpServletBean: init(),内部调用initServletBean完成Servlet的初始化
  
  FrameworkServlet 重写 initServletBean()方法,方法内部调用 initWebApplicationContext()方法完成与DispatcherServlet关联的web应用上下文的初始化工作,应用环境的基础环境的初始化工作由子类DispatcherServlet的onRefresh方法完成。
  
  DispatcherServlet.onRefresh方法内部调用:initStrategies方法完成Springmvc需要的各种组件的初始化工作,至此springmvc可提供完整的外部应用访问功能。
  
  protected void initStrategies(ApplicationContext context) {
  
  initMultipartResolver(context);
  
  initLocaleResolver(context);
  
  initThemeResolver(context);
  
  initHandlerMappings(context);
  
  initHandlerAdapters(context);
  
  initHandlerExceptionResolvers(context);
  
  initRequestToViewNameTranslator(context);
  
  initViewResolvers(context);
  
  initFlashMapManager(context);
  
  }
  
  springmvc 默认配置文件:DispatcherServlet.properties
  
  Springmvc 客户端请求处理过程
  
  核心方法:doService,本质是对Serlvet接口的service方法的最终实现。
  
  doService:完成应用请求的数据封装封装,内部调用doDispatch方法完成请求调度调度处理
  
  源码分析:
  
  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 || mappedHandler.getHandler() == null) {
  
  noHandlerFound(processedRequest, response);
  
  return;
  
  }
  
  // 获取能够支持请求处理器的处理器适配器
  
  HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
  
  // Process last-modified header, if supported by the handler.
  
  String method = request.getMethod();
  
  boolean isGet = "GET".equals(method);
  
  if (isGet || "HEAD".equals(method)) {
  
  long lastModified www.yongshiyule178.com= ha.getLastModified(request, mappedHandler.getHandler());
  
  if (logger.isDebugEnabled()) {
  
  String requestUri = urlPathHelper.getRequestUri(request);
  
  logger.debug("Last-Modified www.xinghaiyule1.com value for [" + requestUri + "] is: " + lastModified);
  
  }
  
  if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
  
  return;
  
  }
  
  }
  
  //前置拦截器拦截请求
  
  if (!mappedHandler.applyPreHandle(processedRequest, response)) {
  
  return;
  
  }
  
  try {
  
  // 处理器适配器将请求传递给控制器处理,获取ModelView对象
  
  mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
  
  }
  
  finally {
  
  if (asyncManager.isConcurrentHandlingStarted(dasheng178.com )) {
  
  return;
  
  }
  
  }
  
  //设置视图名
  
  applyDefaultViewName(request, mv);
  
  //后置拦截器拦截请求
  
  mappedHandler.applyPostHandle(processedRequest, response, mv);
  
  }
  
  catch (Exception ex) {
  
  dispatchException = ex;
  
  }
  
  //处理控制器返回的结果ModelAndView,主要是讲model的数据填充view,渲染后将数据返回给客户端显示
  
  processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
  
  }
  
  catch (Exception www.tiaotiaoylzc.com ex) {
  
  triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
  
  }
  
  catch (Error err) {
  
  triggerAfterCompletionWithError(processedRequest, response, mappedHandler, err);
  
  }
  
  finally {
  
  if (asyncManager.isConcurrentHandlingStarted()) {
  
  // Instead of postHandle and afterCompletion
  
  mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
  
  return;
  
  }
  
  // Clean up any resources used by a multipart request.
  
  if (multipartRequestParsed)www.mytxyl1.com {
  
  cleanupMultipart(processedRequest);
  
  }
  
  }
  
  }
  
  springmvc 请求响应:
  
  主要是将MappingHandler返回的ModelAndView种的Model数据填充到view,并借助于servlet容器(例如tomcat)呈现给客户端。
  
  AbstractView. render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response):
  
  //具体执行代码
  
  renderMergedOutputModel(mergedModel, request, response);
  
  案例分析:内部资源视图(例JSP) InternalResourceView. renderMergedOutputModel
  
  protected void renderMergedOutputModel(
  
  Map<String, Object>www.jtujbo.com model, HttpServletRequest request, HttpServletResponse response) throws Exception {
  
  //封装客户端可视的请求资源数据
  
  HttpServletRequest requestToExpose = getRequestToExpose(request);
  
  exposeModelAsRequestAttributes(model, requestToExpose);
  
  //客户端请求添加辅助信息
  
  exposeHelpers(requestToExpose);
  
  // 获取请求转发路径
  
  String dispatcherPath = prepareForRendering(requestToExpose, response);
  
  //获取目标资源的请求调度器
  
  RequestDispatcher rd = getRequestDispatcher(requestToExpose, dispatcherPath);
  
  if (rd == null) {
  
  throw new ServletException(www.yongshi123.cn"Could not get RequestDispatcher for [" + getUrl() +
  
  "]: Check that the corresponding file exists within your web application archive!");
  
  }
  
  //如果一个URL的内部请求则进行响应资源追加,例如网页内的图片链接等
  
  if (useInclude(requestToExpose, response)) {
  
  response.setContentType(getContentType());
  
  if (logger.isDebugEnabled()) {
  
  logger.debug("Including resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
  
  }
  
  rd.include(requestToExpose, response);
  
  }
  
  else {
  
  // Note: The forwarded resource is supposed to determine the content type itself.
  
  if (logger.isDebugEnabled(www.fengshen157.com)) {
  
  logger.debug("Forwarding to resource [" + getUrl(www.xtd912.com) + "] in InternalResourceView '" + getBeanName() + "'");
  
  }
  
  //repuest请求转发,说明springmvc采用的转发而不是重定向的方式
  
  rd.forward(requestToExpose, response);
  
  }

【spring】- springmvc 工作原理的更多相关文章

  1. springmvc工作原理和环境搭建

    SpringMVC工作原理     上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServle ...

  2. [Java] SpringMVC工作原理之一:DispatcherServlet

    一.DispatcherServlet 处理流程 在整个 Spring MVC 框架中,DispatcherServlet 处于核心位置,它负责协调和组织不同组件完成请求处理并返回响应工作.在看 Di ...

  3. SpringMVC工作原理详解

    先来看一下什么是 MVC 模式 MVC 是一种设计模式. MVC 的原理图如下: SpringMVC 简单介绍 SpringMVC 框架是以请求为驱动,围绕 Servlet 设计,将请求发给控制器,然 ...

  4. SpringMVC 工作原理详解

    本文Github开源项目https://github.com/Snailclimb/JavaGuide,只供自己学习总结无商业用途,如有侵权,联系删除 先来看一下什么是 MVC 模式 MVC 是一种设 ...

  5. Spring MVC工作原理(好用版)

    Spring MVC工作原理 参考: SpringMVC工作原理 - 平凡希 - 博客园https://www.cnblogs.com/xiaoxi/p/6164383.html SpringMVC的 ...

  6. Spring Session工作原理

    本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/KCOFv0nRuymkX79-RZi9eg 作者:张正林 目录:1.引入背景2.使用方法3.工作流程 ...

  7. 笔面试复习(spring常用.jar包/事务/控制反转/bean对象管理和创建/springMVC工作原理/sql查询)

    ###spring常用jar包1.spring.jar是包含有完整发布模块的单个jar包.2.org.springframework.aop包含在应用中使用Spring的AOP特性时所需要的类.3.o ...

  8. springmvc工作原理以及源码分析(基于spring3.1.0)

    springmvc是一个基于spring的web框架.本篇文章对它的工作原理以及源码进行深入分析. 一.springmvc请求处理流程 二.springmvc的工作机制 三.springmvc核心源码 ...

  9. Spring MVC工作原理 及注解说明

    SpringMVC框架介绍 1) spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. Spring 框架提供了构建 Web 应用程序的全功 ...

  10. Spring MVC工作原理及源码解析(三) HandlerMapping和HandlerAdapter实现原理及源码解析

    1.HandlerMapping实现原理及源码解析 在前面讲解Spring MVC工作流程的时候我们说过,前端控制器收到请求后会调⽤处理器映射器(HandlerMapping),处理器映射器根据请求U ...

随机推荐

  1. InsertionSort 直接插入排序(java)

    排序思想: 相当于一堆数字,一开始先取出2个数排序,2个数排好序之后,再从一堆数字里面取一个数排序,直到结束伪代码: INSERTION_SORT(A) for j = 2 to A.length k ...

  2. python多线程创建与使用(转)

    原文:http://codingpy.com/article/python-201-a-tutorial-on-threads/ 创建多线程 创建多线程主要有2种方式. 使用threading.Thr ...

  3. Hyperledger Fabic中的Transaction流程

    Hyperledger Fabic中的Transaction流程 Transaction处理流程时从client发起proposal到背书节点,背书节点发返回背书结果.client再将proposal ...

  4. node http模块搭建简单的服务和客户端

    node-http Node.js提供了http模块,用于搭建HTTP服务端和客户端. 创建Web服务器 server.js /** * node-http 服务端 */ let http = req ...

  5. python循环综合运用

    循环很重要,计算机很蠢,唯一的优势就是按照指令不停的执行,所以决定在说一下. break语句,用在循环体中,迫使循环立即终止,即跳出所在循环体,继续执行循环体后面的语句. sum=0 i=1 whil ...

  6. Python之并发编程-concurrent

    方法介绍 #1 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 ProcessPoolExecutor: 进程池 ...

  7. 树莓派3+rtl8812au开启monitor模式

    首先要有一块树莓派,要有一块rtl8812au的网卡. 这个网卡是支持monitor模式的,但是我原来装的驱动驱动在raspbian上开启monitor模式时提示,找不到设备. 然后换了一个驱动 ht ...

  8. Daily Scrum (2015/10/29)

    今天晚上我们学霸项目的三个小组在一起开会,讨论如何能在后期使我们三个项目更好地结合在一起.为了三个小组的能够同时工作,不出现某一小组因需要其他小组成果而停滞的情况,我们决定围绕lucene,solr, ...

  9. so easy, too happy

    一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 • Estimate • 估计这个任务需要多 ...

  10. Runtime 类的使用

    package com.System.Runtime; import java.io.IOException; /* RunTime 该类类主要代表了应用程序运行的环境. getRuntime() 返 ...