Spring MVC从HandlerMapping中获取Handler

        我们常用的@Controller和@RequestMapping注解对应的HandlerMapping类是RequestMappingHandlerMapping,其继承的AbstractHandlerMethodMapping类中缓存了URL到HandlerMethod的映射信息,从RequestMappingHandlerMapping中获取的Handler实际上是HandlerMethod。源码解析如下:

        HandlerMapping的getHandler()方法由抽象类AbstractHandlerMapping实现:

	@Override
	@Nullable
	public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
        //根据request获取handler,此处handler是HandlerMethod
		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 = obtainApplicationContext().getBean(handlerName);
		}
        //获取执行链
		HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
		if (CorsUtils.isCorsRequest(request)) {
			CorsConfiguration globalConfig = this.globalCorsConfigSource.getCorsConfiguration(request);
			CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
			CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
			executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
		}
		return executionChain;
	}

        AbstractHandlerMethodMapping中getHandlerInternal()方法具体实现:

	@Override
	protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
		//获取path,从request获取contextPath+RequestUri+RemainingPath
        String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
		if (logger.isDebugEnabled()) {
			logger.debug("Looking up handler method for path " + lookupPath);
		}
        //加读锁
		this.mappingRegistry.acquireReadLock();
		try {
            //根据path找HandlerMethod,从mappingRegistry.urlLookup()中根据url找到
            //对应的List<RequestMappingInfo>
            //遍历RequestMappingInfo,RequestMappingInfo中有各种condition,根据        
            //request看各个RequestMappingInfo与request是否匹配,匹配的话构造一个新
            //的RequestMappingInfo返回,根据新构造的RequestMappingInfo和
            //从MappingRegistry.getMappings中根据request获取的HandlerMethod,
            //构造Match对象,并加入到List<Match>中。
			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);
		}
		finally {
            //释放锁
			this.mappingRegistry.releaseReadLock();
		}
	}

                

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值