Spring 源码解析之HandlerMapping源码解析(一)
前言
这个是spring源码解析的第一篇,全文围绕着DispatcherServlet进行展开,Spring 的初始化基本都是通过DispatcherServlet进行初始化的,Spring boot除外,Spring boot是先初始化一个容器之后再初始化的DispatcherServlet,本文首先介绍Spring的HandlerMapping,本文的主要流程围绕着请求流程展开,而不是初始化流程,后续会按照流程的形式对源码进行解读,个人感觉这种方式比较容易理解。阅读源码的原因就是想了解Spring每个流程的实现,以及学习到更深层次的东西。
1.HandlerMapping功能分析
HandlerMapping是spring中最重要的一个类,主要功能就是为请求找到合适的处理器,现在Controller只是处理器的一种,目前我们自己公司自己实现了一套框架,前端js可以直接调用后端 soa service,忽略controller的存在,适合一些无用户状态的场景,比如说rest接口。
先来介绍下目前spring 已经提供的handlermapping
1.BeanNameUrlHandlerMapping
请求路径到bean name的映射
2.SimpleUrlHandlerMapping
最原始的spring使用方式,根struts差不多,通过配置请求路径对应处理的controller类,如下面例子
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/listCourses.go">listCoursesController</prop>
<prop key="/login.go">loginController</prop>
<props>
<property>
<bean>
3.ControllerBeanNameHandlerMapping
This is similar to BeanNameUrlHandlerMapping but doesn’t expect bean names to follow the URL convention: It turns plain bean names into URLs by prepending a slash and optionally applying a specified prefix and/or suffix. However, it only does so for well-known controller types, as listed above (analogous to ControllerClassNameHandlerMapping).
4.ControllerClassNameHandlerMapping
这个主要是类名映射,看看下面就知道了
* WelcomeController -> /welcome*
* HomeController -> /home*
这种方式通常搭配着MultiActionController
,比如/abc/to_add.do 这个表示调用AbcController.java下的to_add的方法。前提是AbcController.java继承MultiActionController
5.RequestMappingHandlerMapping(3.2之前是使用DefaultAnnotationHandlerMapping 这个去处理,但是后来弃用了)
这个就是现在默认的请求匹配方式,通过@RequestMapping 注解来决定调用方法和具体的类,也是最常用的一种。
DispatcherServlet初始化的时候会调用initHandlerMappings()
进行初始化
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
private void initHandlerMappings(ApplicationContext context) {
this.handlerMappings = null;
if (this.detectAllHandlerMappings) {
Map<String, HandlerMapping> matchingBeans =
BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
if (!matchingBeans.isEmpty()) {
this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());
AnnotationAwareOrderComparator.sort(this.handlerMappings);
}
}
else {
try {
HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class);
this.handlerMappings = Collections.singletonList(hm);
}
catch (NoSuchBeanDefinitionException ex) {
}
}
if (this.handlerMappings == null) {
this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class);
if (logger.isDebugEnabled()) {
logger.debug("No HandlerMappings found in servlet '" + getServletName() + "': using default");
}
}
}
- 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
下面是初始化之后的一个状态,我这边使用的是spring boot启动,大致会初始化10个handlermapping,如下图所示:
下图可以看到,默认的RequestMappingHandlerMapping
已经把所有路径映射进去,相关的spring拦截器也放到了RequestMappingHandlerMapping
中,由此说来,拦截器其实是跟RequestMappingHandlerMapping
进行关联的。
2. HandlerMapping流程分析
HandlerMapping流程需要弄清楚几个类的定义:
1. HandlerExecutionChain
(Handler execution chain, consisting of handler object and any handler interceptors. Returned by HandlerMapping’s)根据英文的翻译来说HandlerExecutionChain
是 handler的执行链,由一些handler object 和 handler interceptors组成,这个里面包含了interceptor的所有执行,包括这个请求是否通过preHandle,大致逻辑如下
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = 0; i < interceptors.length; i++) {
HandlerInterceptor interceptor = interceptors[i];
if (!interceptor.preHandle(request, response, this.handler)) {
triggerAfterCompletion(request, response, null);
return false;
}
this.interceptorIndex = i;
}
}
return true;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
2. HandlerAdapter
HandlerAdapter
这个是spring的核心功能,所有的请求对应Controller方法都是通过HandlerAdapter 大该作用就是进行方法映射和调用并且返回结果,对应spring中正常用法实现类是HttpRequestHandlerAdapter
,一般基本的http请求都是通过这个进行处理的。
请求处理流程分析
先来看看这段代码,spring的核心。所有请求调用都是通过这里进行的,可以说是关键路径
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 {
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(processedRequest, response);
return;
}
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
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;
}
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()) {
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}
- 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
- 85
- 86
- 87
- 88
- 89
下图是上述代码的一个大致流程
总结
遗留问题如下:
1. getHandler(processedRequest)
这个方法是如何查找到对应处理的HandlerExecutionChain和HandlerMapping的,比如说静态资源的处理和请求的处理肯定是不同的HandlerMapping
2. getHandlerAdapter(mappedHandler.getHandler());
如果取到对应的HandlerAdapter