DiapatcherServlet 源码分析(四)

来源:互联网 发布:sql select 字段赋值 编辑:程序博客网 时间:2024/05/21 10:22

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() + "'");}//根据请求从缓存中找到对应的handlerHandlerExecutionChain 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;}


3.AbstractUrlHandlerMapping.getHandlerInternal(HttpServletRequest request)
遍历初始化时包装好的handlerMap,解析出对应的Conroller, 并包装成HandlerExecutionChain

protected Object getHandlerInternal(HttpServletRequest request) throws Exception {String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);Object handler = lookupHandler(lookupPath, request);......return handler;}



原创粉丝点击