SpringMVC源码剖析-SpringMVC执行流程

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值