}
}
return null;
}
public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
//找到Handler
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);
}
//把handler封装成HandlerExecutionChain ,其中还包括了拦截器的封装
return getHandlerExecutionChain(handler, request);
}
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
//创建一个Handler执行链对象
HandlerExecutionChain chain =
(handler instanceof HandlerExecutionChain) ?
(HandlerExecutionChain) handler : new HandlerExecutionChain(handler);
//添加拦截器到HandlerExecutionChain
chain.addInterceptors(getAdaptedInterceptors());
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
for (MappedInterceptor mappedInterceptor : mappedInterceptors) {
if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
chain.addInterceptor(mappedInterceptor.getInterceptor());
}
}
return chain;
}
getHandler方法最终会走到org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#getHandlerInternal ,该方法就是根据request查找一个HandlerMethod
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
//拿到请求的路径
String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
if (logger.isDebugEnabled()) {
logger.debug("Looking up handler method for path " + lookupPath);
}
//根据请求路径,找到匹配的HandlerMethod
HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
if (logger.isDebugEnabled()) {
if (handlerMethod != null) {
logger.debug(“Returning handler method [” + handlerMethod + “]”);
}
else {
logger.debug(“Did not find handler method for [” + lookupPath + “]”);
}
}
return (handlerMethod != null) ? handlerMethod.createWithResolvedBean() : null;
}
这里在拿到请求的路径,根据请求路径,找到匹配的HandlerMethod,再往里面走
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
//matches是requestMppingInfo和HandlerMethod的匹配器集合
List matches = new ArrayList();
//根据请求的路径,从 this.urlMap中拿到RequestMappingInfo,
//urlMap 是一个LinkedHashMap,存储了Url路径和RequestMappingInfo的映射关系
List directPathMatches = this.urlMap.get(lookupPath);
if (directPathMatches != null) {
addMatchingMappings(directPathMatches, matches, request);
}
if (matches.isEmpty()) {
// No choice but to go through all mappings
//如果从urlMap中没有找到RequestMappingInfo,那么matches就是空的
//根据Request对象封装一个requestMppingInfo,然后handlerMethods的keySet中拿到对应的HandlerMethod,(会根据Request中的URL,请求方式,参数params,请求头headers,consumes,produces去匹配handlerMethods.keySet中的requestMppingInfo)
//然后从handlerMethods根据requestMppingInfo拿到HandlerMethod
//把requestMppingInfo和HandlerMethod封装成Match添加到matches中
addMatchingMappings(this.handlerMethods.keySet(), matches, request);
}
if (!matches.isEmpty()) {
Comparator comparator = new MatchComparator(getMappingComparator(request));
Collections