Spring4.0.6 源码解读-DispatcherServlet
来源:互联网 发布:淘宝xbox one手柄 编辑:程序博客网 时间:2024/05/13 19:57
Spring mvc 控制层分派流程!
public class DispatcherServlet extends FrameworkServlet {
/**
* 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
*/
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 {
// 1.检查是否是请求是否是multipart
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
// 2.为当前请求获取 HandlerExecutionChain
/*
执行步骤
2.1 根据url请求 返回对应方法
a)
继承关系
AbstractHandlerMapping
AbstractHandlerMethodMapping
b)RequestMappingHandlerMapping
调用RequestMappingHandlerMapping,执行
AbstractHandlerMethodMapping 中的 getHandlerInternal()方法
根据request url 找到 handler method(注解RequestMapping对应的方法) 并返回
2.2 返回控制层实例
a)
继承关系
AbstractBeanFactory
AbstractAutowireCapableBeanFactory
b)DefaultListableBeanFactory
调用DefaultListableBeanFactory,执行
AbstractBeanFactory doGetBean() 方法;
根据 Object sharedInstance = getSingleton(beanName) 判断是否为单例模式
最终返回控制层实例
*/
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
// 3.为当前请求获取 HandlerAdapter
// 处理器适配,即将我们的处理器包装成相应的适配器(从而支持多种类型的处理器)
/*
继承关系
HandlerAdapter
AbstractHandlerMethodAdapter
RequestMappingHandlerAdapter
AnnotationMethodHandlerAdapter
HttpRequestHandlerAdapter
SimpleControllerHandlerAdapter
SimpleServletHandlerAdapter*/
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;
}
try {
// Actually invoke the handler.
// 4.根据3中获得的适配器(HandlerAdapter)实际执行处理
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
}
applyDefaultViewName(request, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
catch (Exception ex) {
dispatchException = ex;
}
// 5.解析视图并进行视图的渲染
/*
5.1 调用 render(mv, request, response);
a)继承关系为:
AbstractCachingViewResolver
UrlBasedViewResolver
InternalResourceViewResolver
b)执行步骤
根据 mv.isReference() 是否 resolve the view name;
true : view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
this.viewResolvers 基于配置文件配置的
(如:BeanNameViewResolver、UrlBasedViewResolver、InternalResourceViewResolver等)
根据ViewResolvers 调用各自resolveViewName(viewName, locale) 方法,返回相应的View
for (ViewResolver viewResolver : this.viewResolvers) {
View view = viewResolver.resolveViewName(viewName, locale);
if (view != null) {
return view;
}
}
如: ViewResolver 是 UrlBasedViewResolver、InternalResourceViewResolver,
则:执行AbstractCachingViewResolver 中的 resolveViewName(String viewName, Locale locale)方法
false :view = mv.getView();
*/
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
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
return;
}
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}
0 0
- Spring4.0.6 源码解读-DispatcherServlet
- spring学习笔记之DispatcherServlet源码解读
- SpringMVC源码之解读DispatcherServlet初始化流程
- SpringMVC工作原理解读--DispatcherServlet类源码解读(初级)
- Spring MVC DispatcherServlet 源码解读View的创建(Velocity)
- Spring4 MVC 笔记: ControllerClassNameHandlerMapping 实现DispatcherServlet
- DispatcherServlet源码剖析
- 【Spring】DispatcherServlet源码分析
- 【Spring】DispatcherServlet源码分析
- 【Spring】DispatcherServlet源码分析
- SpringMVC中DispatcherServlet默认Bean(spring4.3.2)
- Spring4.3.8MVC中文文档-22.2-The DispatcherServlet-前置控制器
- spring4.0源码下载
- Spring4源码下载
- Spring4源码分析(ClassPathXmlApplicationContext)
- Spring MVC:DispatcherServlet源码结构
- DispatcherServlet 源码阅读(1)
- Spring中DispatcherServlet类源码
- hibernate auto=update 还是无法自动创建表的原因
- ubuntu 定时执行任务
- 教你如何在老板面前建立良好的形象
- 深入浅出 - Android系统移植与平台开发(十四) - Sensor HAL框架分析之四
- what it
- Spring4.0.6 源码解读-DispatcherServlet
- 数据库:动态代理
- LINUX最大线程数及最大进程数
- 写一个名为my2DAlloc的函数,用它开辟一个二维数组
- 参数学习算法之梯度下降
- http://www.cnblogs.com/menlsh/archive/2013/05/19/3087810.html
- 应用Valgrind Linux程序的内存问题
- ftp命令
- Android ANR 起因的探究