spring MVC mybatis dispacherServlet(源码解读)
以下源码版本(4.2.0.RELEASE)
dispacherServlet是servlet的实现类,是spring MVC的前端转发器,是spring MVC的核心。
那么它做了哪些事呢?
它主要做了两件事:
NO1:
看如下源码:
/**
* Initialize the strategy objects that this servlet uses.
* <p>May be overridden in subclasses in order to initialize further strategy objects.
*/
protected void initStrategies(ApplicationContext context) {
//初始化分段上传解析器(成对文件上传的解析和封装工作),没有默认的实现
initMultipartResolver(context);
//初始化地域解析器,默认实现是AcceptHeaderLocaleResolver
initLocaleResolver(context);
//初始化主题解析器,默认实现是FixedThemeResolver
initThemeResolver(context);
//初始化处理器映射,这是个集合, 默认实现是BeanNameUrlHandlerMapping和DefaultAnnotationHandlerMapping
initHandlerMappings(context);
//初始化处理器适配器,这是个集合,默认实现是HttpRequestHandlerAdapter,SimpleControllerHandlerAdapter和AnnotationMethodHandlerAdapter
initHandlerAdapters(context);
//初始化处理器异常解析器,这是个集合,默认实现是AnnotationMethodHandlerExceptionResolver,ResponseStatusExceptionResolver和DefaultHandlerExceptionResolver
initHandlerExceptionResolvers(context);
//初始化请求到视图名解析器,默认实现是DefaultRequestToViewNameTranslator
initRequestToViewNameTranslator(context);
//初始化视图解析器,这是个集合,默认实现是InternalResourceViewResolver
initViewResolvers(context);
}
NO2:
每次请求都会调用它的doService方法,在doService方法中调用它的doDispatch方法。
doService方法可看代码中的两处注释:

重点在于调用它的doDispatch方法:
doDispatch(request, response);
首先我们来看方法doDispatch的注释:

可以简单理解成:所有的请求都将在这个方法里映射到指定的action的指定的方法(前提:请求路径正确)。
我们来一步一步解析这个方法:
以下,为
doDispatch方法的源码:
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
// processedRequest是经过checkMultipart方法处理过的request请求
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);
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
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 = ha.getLastModified(request, mappedHandler.getHandler());
if (logger.isDebugEnabled()) {
logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
}
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
catch (Exception ex) {
dispatchException = ex;
}
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}
catch (Error err) {
triggerAfterCompletionWithError(processedRequest, response, mappedHandler, err);
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}
第一行新建的变量processedRequest 会在
后面某一行(在doDispatch方法中,如下:)
processedRequest = checkMultipart(request);
中被方法处理加工,checkMultipart方法也是dispacherServlet类中的一个方法,源码如下:

那么这个方法的作用是什么呢?
关键是下面这一句:
this.multipartResolver != null && this.multipartResolver.isMultipart(request)
这一句代码做了如下两个判断:
1.dispatcherServlet中初始化后的multipartResolver是否为空(配置文件中是否配置,初始化dispatcherServlet时没有默认实例);
2.multipartResolver的方法isMultipart的作用是判断本次请求是否为文件上传。
如下为isMultipart的源码:

通过注释,可简单理解为:该方法通常以检查content-type是否为multipart/form-data的方式判断本次请求是否为文件上传,但实际功能需要看具体解析器对该方法的实现细节。
checkMultipart方法大致如此,再具体一些的内容在这里我就不赘述了,有兴趣的朋友可以自己看源码。
(多嘴一句:若想使用multipartResolver不要忘了增加jar包支持:commons.fileupload,common.io).
---------------------------------------
方法doDispatch的第二行新建了变量mappedHandler ,
类型为HandlerExecutionChain ,通过查看该类的源码,可清楚其作用,如下:

可理解其为一个处理链,包含了所有的handler和拦截器。
以下三行代码为doDispatch的核心:
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
通过注释,可理解前一句的作用:找到指定的handler,
后一句:找到对应的适配器adapter,
第三句:在这个函数里面会真正执行request请求相对于handler的方法(这只是大概的代码流程,真正的调用方法之前还有很多先前处理。)
以下是我debug断点的显示数据:

该方法时DispatcherServlet类中的方法getHandler,可以看到这里会对所有handlerMapper进行遍历,后执行HandlerMappering的getHandler方法,
如下:为HandlerMappering的getHandler方法:

这个方法会返回一个handler和所有的拦截器,组装成了一个HandlerExecutionChain类。也就是在方法doDispatch第二行代码里定义的mappedHandler,
该方法的具体实现是在org.springframework.web.servlet.handler.AbstractHandlerMapping中,如下:
/**
* Look up a handler for the given request, falling back to the default
* handler if no specific one is found.
* @param request current HTTP request
* @return the corresponding handler instance, or the default handler
* @see #getHandlerInternal
*/
@Override
public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
Object handler = getHandlerInternal(request);
if (handler == null) {
handler = getDefaultHandler();
}
if (handler == null) {
return null;
}
// Bean name or resolved handler?
if (handler instanceof String) {
String handlerName = (String) handler;
handler = getApplicationContext().getBean(handlerName);
} HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
if (CorsUtils.isCorsRequest(request)) {
CorsConfiguration globalConfig = this.corsConfigSource.getCorsConfiguration(request);
CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
}
return executionChain;
}
这里的getHandlerInternal是个抽象方法,有具体的HandlerMapping来实现,获取到的handler如果为空,则获取默认配置的handler,若为string类型,则表示要去spring配置容器中去找这个bean,
该方法剩下的部分正如HandlerMappering的getHandler方法注释所描述的:返回一个handler和所有的拦截器(有兴趣可深入了解)。
--------------------------
再来看doDispath方法里的
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
它首先调用了dispatcherServlet里的 getHandlerAdapter方法;看其源码如下:

看其注释:该方法会通过已确定映射的handler类来寻找满足条件的adapter,(注意,若没有找到,会直接报servletException异常)。
这个方法的核心是 方法supports,这是个抽象方法,不同的适配器会有不同的内部细节,如下分别为
SimpleControllerHandlerAdapter和SimpleServletHandlerAdapter的实现:


通过supports方法,dispatcherervlet类可以确定本次请求request映射的adapter.
------------------------
最后看
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
这里的mv是一个ModelAndView类,ha是前面获得的适配器adapter,
方法handler()是个抽象方法,具体实现细节依赖于适配器adapter的实现类,
但我门可看其注释,理解该方法的作用,
源码如下:

其注释的意思是是:使用提供的handler(前面确定的handler)来处理这个request请求。
以下,已实现类SimpleControllerHandlerAdapter为例,

这里会执行handlerRequest(该方法的实现细节依赖于不同的实现类,最后返回一个ModelAndAndView。
以上,为我本次研读dispatcherServlet类的所得。
可总结为,dispatcherServlet根据默认或配置的handlermapping和adapter,选择不同的方式对请求request请求进行处理,最后返回一个视图ModeAndView。
(那么返回值为json是如何实现的?····问题越来越多了)
----------------------------------补充----------------------------
可以看出,关于
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
部分,阐述的比较模糊,或者说,基本没什么描述,虽然这部分是核心,
我试图过阐述其实现细节,但这部分的实现细节根据不同的handlerMapping和不同的adpater而不同,
所以,我准备在其他章节对其进行分别阐述。
spring MVC mybatis dispacherServlet(源码解读)的更多相关文章
- 0002 - Spring MVC 拦截器源码简析:拦截器加载与执行
1.概述 Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理.例如通过拦截器可以进行权限验证.记录请求信息的日 ...
- spring MVC 原理及源码解析
首先要知道springmvc的核心控制器dispatcherServlet(继承自httpServlet) 一般httpServlet的请求过程: 1.初始化(创建servlet实例时)时会执行ser ...
- ssm整合说明与模板-Spring Spring MVC Mybatis整合开发
ssm整合说明 spring+spring mvc+mybatis 说明 源码下载 由于之前存在ssh框架,spring+struts+hibernate,其中spring负责aop与ioc,所以一般 ...
- spring IOC DI AOP MVC 事务, mybatis 源码解读
demo https://gitee.com/easybao/aop.git spring DI运行时序 AbstractApplicationContext类的 refresh()方法 1: pre ...
- Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)
Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码) 备注: 之前在Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合中 ...
- MyBatis源码解读(1)——SqlSessionFactory
在前面对MyBatis稍微有点了解过后,现在来对MyBatis的源码试着解读一下,并不是解析,暂时定为解读.所有对MyBatis解读均是基于MyBatis-3.4.1,官网中文文档:http://ww ...
- MyBatis源码解读之延迟加载
1. 目的 本文主要解读MyBatis 延迟加载实现原理 2. 延迟加载如何使用 Setting 参数配置 设置参数 描述 有效值 默认值 lazyLoadingEnabled 延迟加载的全局开关.当 ...
- Mybatis源码解读-SpringBoot中配置加载和Mapper的生成
本文mybatis-spring-boot探讨在springboot工程中mybatis相关对象的注册与加载. 建议先了解mybatis在spring中的使用和springboot自动装载机制,再看此 ...
- MyBatis源码解读(3)——MapperMethod
在前面两篇的MyBatis源码解读中,我们一路跟踪到了MapperProxy,知道了尽管是使用了动态代理技术使得我们能直接使用接口方法.为巩固加深动态代理,我们不妨再来回忆一遍何为动态代理. 我相信在 ...
随机推荐
- PHP中的数据库一、MySQL优化策略综述
前些天看到一篇文章说到PHP的瓶颈很多情况下不在PHP自身,而在于数据库.我们都知道,PHP开发中,数据的增删改查是核心.为了提升PHP的运行效率,程序员不光需要写出逻辑清晰,效率很高的代码,还要能对 ...
- FTP概述
FTP是什么? 早期三大网络应用之一:ftp(40年).http.mail ftp file transfer protocol 文件传输协议 FTP服务概述 C/S模型 客户端-服务器 FTP采用双 ...
- LeetCode题解-----Sliding Window Maximum
题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...
- android SharedPreferences 存储对象
我们知道SharedPreferences只能保存简单类型的数据,例如,String.int等. 如果想用SharedPreferences存取更复杂的数据类型(类.图像等),就需要对这些数据进行编码 ...
- MongoDB数据库的CURD的一些基本语句
from:http://www.data321.com/shujuku/20160514417/addToSetQianMianBuXuYaoJinXing 插入文档: SQL语句: INSERT I ...
- docker学习(3) 容器的启动过程
这一节我们来稍微了解下docker原理性的东西 docker run -i -t ubuntu /bin/bash 输入上面这行命令,启动一个ubuntu容器时,到底发生了什么? 大致过程可以用下图描 ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- valueOf和toString的区别
基本上所有的JavaScript数据类型都有valueOf(),toString()方法,null除外,这两个方法解决了JavaScript值运算和显示的问题 valueOf()会把数据类型转换成原始 ...