Spring4.0.6 源码解读-DispatcherServlet

 

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

 

RequestMappingHandlerMapping

 

b)
调用RequestMappingHandlerMapping,执行
AbstractHandlerMethodMapping 中的 getHandlerInternal()方法
根据request url 找到 handler method(注解RequestMapping对应的方法) 并返回

2.2 返回控制层实例
a)
继承关系
AbstractBeanFactory

 

AbstractAutowireCapableBeanFactory
DefaultListableBeanFactory

 

b)
调用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);
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值