SpringMVC源码分析(3)DispatcherServlet的请求处理流程
<springmvc源码分析(2)dispatcherservlet的初始化>初始化DispatcherServlet的多个组件。
本文继续分析DispatcherServlet解析请求的过程。
概览

①:DispatcherServlet是springmvc中的前端控制器(front controller),负责接收request并将request转发给对应的处理组件.
②:HanlerMapping是springmvc中完成url到controller映射的组件.DispatcherServlet接收request,然后从HandlerMapping查找处理request的controller.
③:Cntroller处理request,并返回ModelAndView对象,Controller是springmvc中负责处理request的组件(类似于struts2中的Action),ModelAndView是封装结果视图的组件.
④ ⑤ ⑥:视图解析器解析ModelAndView对象并返回对应的视图给客户端.
要点
维护url和controller的映射
这部分工作由DefaultAnnotationHandlerMapping.setApplicationContext的父类
org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext实现。具体方法为detectHandlers
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
protectedvoiddetectHandlers()throwsBeansException{if(logger.isDebugEnabled()){logger.debug("LookingforURLmappingsinapplicationcontext:"+getApplicationContext());}String[]beanNames=(this.detectHandlersInAncestorContexts?BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(),Object.class):getApplicationContext().getBeanNamesForType(Object.class));//TakeanybeannamethatwecandetermineURLsfor.for(StringbeanName:beanNames){String[]urls=determineUrlsForHandler(beanName);if(!ObjectUtils.isEmpty(urls)){//URLpathsfound:Let'sconsideritahandler.registerHandler(urls,beanName);}else{if(logger.isDebugEnabled()){logger.debug("Rejectedbeanname'"+beanName+"':noURLpathsidentified");}}}} |
2.准确定位处理请求的具体方法(在AnnotationMethodHandlerAdapter中实现)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
protectedModelAndViewinvokeHandlerMethod(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{ServletHandlerMethodResolvermethodResolver=getMethodResolver(handler);MethodhandlerMethod=methodResolver.resolveHandlerMethod(request);//具体实现方法的匹配ServletHandlerMethodInvokermethodInvoker=newServletHandlerMethodInvoker(methodResolver);ServletWebRequestwebRequest=newServletWebRequest(request,response);ExtendedModelMapimplicitModel=newBindingAwareModelMap();Objectresult=methodInvoker.invokeHandlerMethod(handlerMethod,handler,webRequest,implicitModel);ModelAndViewmav=methodInvoker.getModelAndView(handlerMethod,handler.getClass(),result,implicitModel,webRequest);methodInvoker.updateModelAttributes(handler,(mav!=null?mav.getModel():null),implicitModel,webRequest);returnmav;} |
1.请求入口
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
@OverrideprotectedfinalvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{processRequest(request,response);}/***DelegatePOSTrequeststo{@link#processRequest}.*@see#doService*/@OverrideprotectedfinalvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{processRequest(request,response);}protectedfinalvoidprocessRequest(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{longstartTime=System.currentTimeMillis();ThrowablefailureCause=null;//ExposecurrentLocaleResolverandrequestasLocaleContext.LocaleContextpreviousLocaleContext=LocaleContextHolder.getLocaleContext();LocaleContextHolder.setLocaleContext(buildLocaleContext(request),this.threadContextInheritable);//ExposecurrentRequestAttributestocurrentthread.RequestAttributespreviousRequestAttributes=RequestContextHolder.getRequestAttributes();ServletRequestAttributesrequestAttributes=null;if(previousRequestAttributes==null||previousRequestAttributes.getClass().equals(ServletRequestAttributes.class)){requestAttributes=newServletRequestAttributes(request);RequestContextHolder.setRequestAttributes(requestAttributes,this.threadContextInheritable);}if(logger.isTraceEnabled()){logger.trace("Boundrequestcontexttothread:"+request);}try{doService(request,response);}catch(ServletExceptionex){failureCause=ex;throwex;}catch(IOExceptionex){failureCause=ex;throwex;}catch(Throwableex){failureCause=ex;thrownewNestedServletException("Requestprocessingfailed",ex);}finally{//Clearrequestattributesandresetthread-boundcontext.LocaleContextHolder.setLocaleContext(previousLocaleContext,this.threadContextInheritable);if(requestAttributes!=null){RequestContextHolder.setRequestAttributes(previousRequestAttributes,this.threadContextInheritable);requestAttributes.requestCompleted();}if(logger.isTraceEnabled()){logger.trace("Clearedthread-boundrequestcontext:"+request);}if(failureCause!=null){this.logger.debug("Couldnotcompleterequest",failureCause);}else{this.logger.debug("Successfullycompletedrequest");}if(this.publishEvents){//Whetherornotwesucceeded,publishanevent.longprocessingTime=System.currentTimeMillis()-startTime;this.webApplicationContext.publishEvent(newServletRequestHandledEvent(this,request.getRequestURI(),request.getRemoteAddr(),request.getMethod(),getServletConfig().getServletName(),WebUtils.getSessionId(request),getUsernameForRequest(request),processingTime,failureCause));}}} |
processRequest方法主要做4项工作。
得到当前线程的LocaleContext和RequestAttributes,创建新的LocaleContext和RequestAttributes并重新绑定到当前线程。
调用子类实现的doService()
重置当前线程的LocaleContext和RequestAttributes
执行成功后,发布ServletRequestHandledEvent事件。
2.DispatcherServlet自定义的doService方法
1234567891011121314151617181920212223242526272829303132333435363738protectedvoiddoService(HttpServletRequestrequest,HttpServletResponseresponse)throwsException{if(logger.isDebugEnabled()){StringrequestUri=urlPathHelper.getRequestUri(request);logger.debug("DispatcherServletwithname'"+getServletName()+"'processing"+request.getMethod()+"requestfor["+requestUri+"]");}//Keepasnapshotoftherequestattributesincaseofaninclude,//tobeabletorestoretheoriginalattributesaftertheinclude.Map<string,object>attributesSnapshot=null;if(WebUtils.isIncludeRequest(request)){logger.debug("Takingsnapshotofrequestattributesbeforeinclude");attributesSnapshot=newHashMap<string,object>();EnumerationattrNames=request.getAttributeNames();while(attrNames.hasMoreElements()){StringattrName=(String)attrNames.nextElement();if(this.cleanupAfterInclude||attrName.startsWith("org.springframework.web.servlet")){attributesSnapshot.put(attrName,request.getAttribute(attrName));}}}//Makeframeworkobjectsavailabletohandlersandviewobjects.request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE,getWebApplicationContext());request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE,this.localeResolver);request.setAttribute(THEME_RESOLVER_ATTRIBUTE,this.themeResolver);request.setAttribute(THEME_SOURCE_ATTRIBUTE,getThemeSource());try{doDispatch(request,response);}finally{//Restoretheoriginalattributesnapshot,incaseofaninclude.if(attributesSnapshot!=null){restoreAttributesAfterInclude(request,attributesSnapshot);}}}</string,object></string,object>主要做两部分工作
如果是include请求,先保存一份request域数据的快照,doDispatch执行过后,将会用快照数据恢复。
调用doDispatch方法,完成请求转发。
3.doDispatch方法
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697protectedvoiddoDispatch(HttpServletRequestrequest,HttpServletResponseresponse)throwsException{HttpServletRequestprocessedRequest=request;HandlerExecutionChainmappedHandler=null;intinterceptorIndex=-1;try{ModelAndViewmv;booleanerrorView=false;try{//1.检查是否是文件上传的请求processedRequest=checkMultipart(request);//Determinehandlerforthecurrentrequest.//2.取得处理当前请求的controller,这里也称为hanlder,处理器,第一个步骤的意义就在这里体现了.//这里并不是直接返回controller,而是返回的HandlerExecutionChain请求处理器链对象,该对象封装了handler和interceptors.mappedHandler=getHandler(processedRequest,false);if(mappedHandler==null||mappedHandler.getHandler()==null){noHandlerFound(processedRequest,response);return;}//Determinehandleradapterforthecurrentrequest.//3.获取处理request的处理器适配器handleradapterHandlerAdapterha=getHandlerAdapter(mappedHandler.getHandler());//Processlast-modifiedheader,ifsupportedbythehandler.Stringmethod=request.getMethod();booleanisGet="GET".equals(method);if(isGet||"HEAD".equals(method)){longlastModified=ha.getLastModified(request,mappedHandler.getHandler());if(logger.isDebugEnabled()){StringrequestUri=urlPathHelper.getRequestUri(request);logger.debug("Last-Modifiedvaluefor["+requestUri+"]is:"+lastModified);}if(newServletWebRequest(request,response).checkNotModified(lastModified)&&isGet){return;}}//ApplypreHandlemethodsofregisteredinterceptors.//4.拦截器的预处理方法HandlerInterceptor[]interceptors=mappedHandler.getInterceptors();if(interceptors!=null){for(inti=0;i<interceptors.length;i++){ actuallyinvokethehandler.=""applyposthandlemethodsofregisteredinterceptors.=""handlerinterceptorinterceptor="interceptors[i];"interceptorindex="i;"inti="interceptors.length-1;i"mv="ha.handle(processedRequest,response,mappedHandler.getHandler());">=0;i--){HandlerInterceptorinterceptor=interceptors[i];interceptor.postHandle(processedRequest,response,mappedHandler.getHandler(),mv);}}}catch(ModelAndViewDefiningExceptionex){logger.debug("ModelAndViewDefiningExceptionencountered",ex);mv=ex.getModelAndView();}catch(Exceptionex){Objecthandler=(mappedHandler!=null?mappedHandler.getHandler():null);mv=processHandlerException(processedRequest,response,handler,ex);errorView=(mv!=null);}//Didthehandlerreturnaviewtorender?if(mv!=null&&!mv.wasCleared()){render(mv,processedRequest,response);if(errorView){WebUtils.clearErrorRequestAttributes(request);}}else{if(logger.isDebugEnabled()){logger.debug("NullModelAndViewreturnedtoDispatcherServletwithname'"+getServletName()+"':assumingHandlerAdaptercompletedrequesthandling");}}//Triggerafter-completionforsuccessfuloutcome.triggerAfterCompletion(mappedHandler,interceptorIndex,processedRequest,response,null);}catch(Exceptionex){//Triggerafter-completionforthrownexception.triggerAfterCompletion(mappedHandler,interceptorIndex,processedRequest,response,ex);throwex;}catch(Errorerr){ServletExceptionex=newNestedServletException("Handlerprocessingfailed",err);//Triggerafter-completionforthrownexception.triggerAfterCompletion(mappedHandler,interceptorIndex,processedRequest,response,ex);throwex;}finally{//Cleanupanyresourcesusedbyamultipartrequest.if(processedRequest!=request){cleanupMultipart(processedRequest);}}}</interceptors.length;i++){>很明显这儿是SpringMVC核心。
1.根据请求的路径找到HandlerMethod(带有Method反射属性,也就是对应Controller中的方法)(DispatcherServlet.getHandler完成)
2.匹配路径对应的拦截器(DispatcherServlet.getHandler完成)
3.获得HandlerExecutionChain对象(DispatcherServlet.getHandler完成)
4.通过HandlerAdapter对象进行处理得到ModelAndView对象(HandlerAdapter.handle)
5.调用HandlerInterceptor.preHandle
6.调用HandlerInterceptor.postHandle
7. 渲染
4.总结

简单粗暴的总结下
step1-6: 获取controller
step5-15 :调用controller方法
step17-20:渲染view
其他:aop方式处理拦截统一处理。
SpringMVC源码分析(3)DispatcherServlet的请求处理流程的更多相关文章
- 2.SpringMVC源码分析:DispatcherServlet的初始化与请求转发
一.DispatcherServlet的初始化 在我们第一次学Servlet编程,学java web的时候,还没有那么多框架.我们开发一个简单的功能要做的事情很简单,就是继承HttpServlet,根 ...
- springmvc源码分析系列-请求处理流程
接上一篇-springmvc源码分析开头片 上一节主要说了一下springmvc与struts2的作为MVC中的C(controller)控制层的一些区别及两者在作为控制层方面的一些优缺点.今天就结合 ...
- springMVC源码分析--DispatcherServlet请求获取及处理
在之前的博客springMVC源码分析--容器初始化(二)DispatcherServlet中我们介绍过DispatcherServlet,是在容器初始化过程中出现的,我们之前也说过Dispatche ...
- SpringMVC源码分析--容器初始化(五)DispatcherServlet
上一篇博客SpringMVC源码分析--容器初始化(四)FrameworkServlet我们已经了解到了SpringMVC容器的初始化,SpringMVC对容器初始化后会进行一系列的其他属性的初始化操 ...
- springMVC源码分析--容器初始化(二)DispatcherServlet
在上一篇博客springMVC源码分析--容器初始化(一)中我们介绍了spring web初始化IOC容器的过程,springMVC作为spring项目中的子项目,其可以和spring web容器很好 ...
- 8、SpringMVC源码分析(3):分析ModelAndView的形成过程
首先,我们还是从DispatcherServlet.doDispatch(HttpServletRequest request, HttpServletResponse response) throw ...
- SpringMVC源码情操陶冶-DispatcherServlet类简析(一)
阅读源码有利于陶冶情操,此文承接前文SpringMVC源码情操陶冶-DispatcherServlet父类简析 注意:springmvc初始化其他内容,其对应的配置文件已被加载至beanFactory ...
- 框架-springmvc源码分析(一)
框架-springmvc源码分析(一) 参考: http://www.cnblogs.com/heavenyes/p/3905844.html#a1 https://www.cnblogs.com/B ...
- [心得体会]SpringMVC源码分析
1. SpringMVC (1) springmvc 是什么? 前端控制器, 主要控制前端请求分配请求任务到service层获取数据后反馈到springmvc的view层进行包装返回给tomcat, ...
随机推荐
- Codeforces 757C. Felicity is Coming!
C. Felicity is Coming! time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- Hadoop的本地库(Native Libraries)介绍
Hadoop是使用Java语言开发的,但是有一些需求和操作并不适合使用java,所以就引入了本地库(Native Libraries)的概念,通过本地库,Hadoop可以更加高效地执行某一些操作. 目 ...
- hbase 单机版安装
1.安装jdk参见http://www.cnblogs.com/lvlv/p/4337863.html 安装路径:/usr/java/jdk1.7.0_79 2.下载hbase http://mi ...
- 设置frameset高度
设置frameset的高度 设置frameset高度 目前做了一个项目,界面如下: 这是使用frameset做的,在宽屏下开发一直没有发现什么问题,直到一个用户使用800*600的机子测试的时候, ...
- 【Linux】OpenSSL 安装
OpenSSL 简介 OpenSSL 是一个安全套接字层密码库,囊括主要的密码算法.常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用. OpenSSL 安装 环境:L ...
- linux_配置三台虚拟机免密登录
在node01上面直接生成公钥和私钥 ssh-keygen --> 四下回车 ll -a 进行查看,发现出现.ssh文件即已经生成 将此node01的公钥拷贝到第二台机器上 ssh-copy-i ...
- thymeleaf使用基础教程
thymeleaf 是新一代的模板引擎,在spring4.0中推荐使用thymeleaf来做前端模版引擎. thymeleaf介绍 简单说, Thymeleaf 是一个跟 Velocity.FreeM ...
- 2019.01.20 bzoj3999: [TJOI2015]旅游(树链剖分)
传送门 树链剖分菜题. 题意不清差评. 题意简述(保证清晰):给一棵带权的树,每次从aaa走到bbb,在走过的路径上任意找两个点,求后访问的点与先访问的点点权差的最大值. 思路: 考虑暴力:维护路径的 ...
- poj-2777(区间线段树,求种类数模板)
题目链接:http://poj.org/problem?id=2777 参考文章:https://blog.csdn.net/heucodesong/article/details/81038360 ...
- kafka 支持发布订阅
概述 一般消息队列的是实现是支持两种模式的,即点对点,还有一种是topic发布订阅者模式,比如ACTIVEMQ.KAFKA也支持这两种模式,但是实现的原理不一样. KAFKA 的消息被读取后,并不是马 ...