springMVC源码笔记
springMVC 设计总览
下图来源:https://www.cnblogs.com/fangjian0423/p/springMVC-directory-summary.html

下图来源:https://www.cnblogs.com/sunniest/p/4555801.html

核心类DispatcherServlet

接收请求DispatcherServlet也是一个Servlet
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
...
try {
this.doDispatch(request, response);
} finally {
if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted() && attributesSnapshot != null) {
this.restoreAttributesAfterInclude(request, attributesSnapshot);
}
}
}
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
...
mappedHandler = this.getHandler(processedRequest);
HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
...
mappedHandler.applyPreHandle(processedRequest, response)
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
...
this.applyDefaultViewName(request, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
this.processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
HandlerAdapter是一个适配器,根据handler适配ModelAndView
getHandler(processedRequest)是为了获取HandlerExecutionChain:protected HandlerExecutionChain getHandler(HttpServletRequest request)
HandlerExecutionChain 有什么:
private static final Log logger = LogFactory.getLog(HandlerExecutionChain.class);
private final Object handler;
private HandlerInterceptor[] interceptors;
private List<HandlerInterceptor> interceptorList;
private int interceptorIndex;
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (this.getInterceptors() != null) {
for(int i = 0; i < this.getInterceptors().length; this.interceptorIndex = i++) {
HandlerInterceptor interceptor = this.getInterceptors()[i];
if (!interceptor.preHandle(request, response, this.handler)) {
this.triggerAfterCompletion(request, response, (Exception)null);
return false;
}
}
}
return true;
}
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {
if (this.getInterceptors() != null) {
for(int i = this.getInterceptors().length - 1; i >= 0; --i) {
HandlerInterceptor interceptor = this.getInterceptors()[i];
interceptor.postHandle(request, response, this.handler, mv);
}
}
}
HandlerInterceptor:拦截器(在handler处理前后执行applyPreHandle,applyPostHandle方法)
handler:处理器
springMVC源码笔记的更多相关文章
- springmvc源码笔记-RequestMappingHandlerMapping
下图是springmvc的执行流程 图片来源:https://www.jianshu.com/p/8a20c547e245 DispatcherServlet根据url定位到Controller和方法 ...
- springmvc源码笔记-HandlerMapping注入
在springmvc中,如何根据url找到controller以及对应方法,依赖的是HandlerMapping接口的getHandler方法 在spring容器中默认注册的HandlerMappin ...
- springmvc源码笔记-HandlerMethodReturnValueHandler
返回值解析器 用于对controller的返回值进行二次处理 结构 // 返回值解析器 public interface HandlerMethodReturnValueHandler { // 判断 ...
- [心得体会]SpringMVC源码分析
1. SpringMVC (1) springmvc 是什么? 前端控制器, 主要控制前端请求分配请求任务到service层获取数据后反馈到springmvc的view层进行包装返回给tomcat, ...
- SpringMVC源码剖析(四)- DispatcherServlet请求转发的实现
SpringMVC完成初始化流程之后,就进入Servlet标准生命周期的第二个阶段,即“service”阶段.在“service”阶段中,每一次Http请求到来,容器都会启动一个请求线程,通过serv ...
- SpringMVC源码分析系列
说到java的mvc框架,struts2和springmvc想必大家都知道,struts2的设计基本上完全脱离了Servlet容器,而springmvc是依托着Servlet容器元素来设计的,同时sp ...
- SpringMVC源码剖析(二)- DispatcherServlet的前世今生
上一篇文章<SpringMVC源码剖析(一)- 从抽象和接口说起>中,我介绍了一次典型的SpringMVC请求处理过程中,相继粉墨登场的各种核心类和接口.我刻意忽略了源码中的处理细节,只列 ...
- springMvc源码学习之:spirngMvc的参数注入的问题
转载:https://my.oschina.net/lichhao/blog/172562 概述 在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成 ...
- SpringMVC 源码深度解析<context:component-scan>(扫描和注冊的注解Bean)
我们在SpringMVC开发项目中,有的用注解和XML配置Bean,这两种都各有自己的优势,数据源配置比較经经常使用XML配置.控制层依赖的service比較经经常使用注解等(在部署时比較不会改变的) ...
随机推荐
- HTML5新增特性
1. 语义化标签 2. 增强型表单 (1)新的表单输入类型 (2)新表单元素 (3)新表单属性 3. 视频和音频 4. Canvas绘图(图形.路径.文本.渐变.图像) 5. SVG绘图 (与Canv ...
- ExcelDna项目完整工程演示及讲解
原始链接:http://www.cnblogs.com/Charltsing/p/ExcelDnaDemo.html ExcelDna工程演示讲课内容 1.ExcelDna是啥? 2.ExcelDna ...
- 其它综合-VMware虚拟机安装Ubuntu 19.04 版本
Ubuntu 19.04 版本安装过程 1. 环境: 使用的虚拟机软件是VMware,版本为 12 .(网上一搜一大推,在此不再演示.) 使用的 ISO镜像为Ubuntu 19.04.(自己也可以在网 ...
- 简单了解uuid
1.含义 UUID-Universally Unique IDentifiers,翻译过来就是“全局唯一标志符”. UUID到底是什么? UUID是一个标帜你系统中的存储设备的字符串,其目的是帮助使用 ...
- Dijkstra算法——计算一个点到其他所有点的最短路径的算法
迪杰斯特拉算法百度百科定义:传送门 gh大佬博客:传送门 迪杰斯特拉算法用来计算一个点到其他所有点的最短路径,是一种时间复杂度相对比较优秀的算法 O(n2)(相对于Floyd算法来说) 是一种单源最短 ...
- JMeter5.1开发TCP协议接口脚本
最简单的方法,就是找开发给报文,直接复制到tcp取样器中,将需要变化的值做参数化就可以了.(xml报文要去掉回车换行) 下面是一个通讯头定义 通讯头56个字节(1个字符一个字节) 3 + 9 + 9 ...
- c 判断文件或文件夹是否存在,多种方法, 为什么从一开始就不直接来个统一的呢?
具体内容,请看: https://blog.csdn.net/u012494876/article/details/51204615 判断文件或文件夹是否存在,竟然有这么多方法: GetFileAtt ...
- IDEA 破解
推荐三篇文章 : 1: https://blog.csdn.net/nishiwodebocai21/article/details/71359619?fps=1&locationNu ...
- server被强制关闭,
一个client和一个Server,两者之间建立了一个基于TCP的socket连接,在刚刚建立好连接后,尚未进行数据传输,Server端应用程序突然crush掉了,现在立刻重启Server端应用程序( ...
- 【Linux网络编程】TCP网络编程中connect()、listen()和accept()三者之间的关系
[Linux网络编程]TCP网络编程中connect().listen()和accept()三者之间的关系 基于 TCP 的网络编程开发分为服务器端和客户端两部分,常见的核心步骤和流程如下: conn ...