1. DiapatcherServlet.getHandler(HttpServletRequest request)
遍历所有的注册handler, 默认为:
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
for (HandlerMapping hm : this.handlerMappings) {
if (logger.isTraceEnabled()) {
logger.trace(
"Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
}
//根据请求从缓存中找到对应的handler
HandlerExecutionChain handler = hm.getHandler(request);
if (handler != null) {
return handler;
}
}
return null;
}
2.AbstractHandlerMapping.getHandler(HttpServletRequest request)
找到匹配的handler
public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
Object handler = getHandlerInternal(request);
......
HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
......
return executionChain;
}
遍历初始化时包装好的handlerMap,解析出对应的Conroller, 并包装成HandlerExecutionChain
protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
Object handler = lookupHandler(lookupPath, request);
......
return handler;
}