微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringMvc @RequestMapping原理

讲这个之前,我们得先知道在SpringMvc启动时,会加载所有的Bean类,就是加了@Controller,@Component等组件标识的类,然后会把@RequestMapping的方法也加入到一个集合。放入到上下文环境中。

发起请求后的执行流程是:检查request类型-->获取匹配的Handlemethod-->查找拦截器-->组成HandlerExecutionChain执行链-->获取方法执行链对象的适配器(HandlerAdapter)-->然后反射执行业务方法

一个大概的了解之后,开始源码分析:

protected void dodispatch(HttpServletRequest request,HttpServletResponse response) throws Exception {
        HttpServletRequest processedRequest = request;
        HandlerExecutionChain mappedHandler = null;
        boolean multipartRequestParsed = false;

        WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

        try {
            ModelAndView mv = ;
            Exception dispatchException = ;

             {
                processedRequest = checkMultipart(request);
                multipartRequestParsed = processedRequest != request;

                // Determine handler for the current request.
          //获取匹配的执行链
mappedHandler = getHandler(processedRequest); if (mappedHandler == null || mappedHandler.getHandler() == ) { noHandlerFound(processedRequest,response); return; } Determine handler adapter for the current request.
//根据匹配到的执行链对象,获取合适的适配器
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler()); Process last-modified header,if supported by the handler. String method = request.getmethod(); boolean isGet = "GET".equals(method); if (isGet || "HEAD".equals(method)) { long lastModified = ha.getLastModified(request,mappedHandler.getHandler()); if (logger.isDebugEnabled()) { String requestUri = urlPathHelper.getRequestUri(request); logger.debug("Last-Modified value for [" + requestUri + "] is: " + lastModified); } if (new ServletWebRequest(request,response).checkNotModified(lastModified) && isGet) { ; } } if (!mappedHandler.applyPreHandle(processedRequest,response)) { { Actually invoke the handler.
            //在这里面反射执行业务方法
mv = ha.handle(processedRequest,response,mappedHandler.getHandler()); } finally (asyncManager.isConcurrentHandlingStarted()) { ; } } applydefaultviewName(request,mv); mappedHandler.applyPostHandle(processedRequest,mv); } catch (Exception ex) { dispatchException = ex; } processdispatchResult(processedRequest,mappedHandler,mv,dispatchException); } (Exception ex) { triggerAfterCompletion(processedRequest,ex); } (Error err) { triggerAfterCompletionWithError(processedRequest,err); } { (asyncManager.isConcurrentHandlingStarted()) { Instead of postHandle and afterCompletion mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest,response); ; } Clean up any resources used by a multipart request. (multipartRequestParsed) { cleanupMultipart(processedRequest); } } }

从标红部分点进去,到AbstractHandlerMethodAdapter类的handle方法-->RequestMappingHandlerAdapter类的handleInternal方法-->RequestMappingHandlerAdapter类的invokeHandleMethod

-->RequestMappingHandlerAdapter类的invokeAndHandle-->ServletinvocableHandlerMethod类的invokeForRequest-->invocableHandlerMethod类的invokeForRequest。该方法代码如下:

public final Object invokeForRequest(NativeWebRequest request,ModelAndViewContainer mavContainer,Object... providedArgs)  Exception {

        Object[] args = getmethodArgumentValues(request,mavContainer,providedArgs);
         (logger.isTraceEnabled()) {
            StringBuilder sb = new StringBuilder("Invoking [");
            sb.append(this.getBeanType().getSimpleName()).append(".");
            sb.append(getmethod().getName()).append("] method with arguments ");
            sb.append(Arrays.asList(args));
            logger.trace(sb.toString());
        }
     //执行方法 Object returnValue
= invoke(args); (logger.isTraceEnabled()) { logger.trace("Method [" + getmethod().getName() + "] returned [" + returnValue + "]"); } returnValue; }

然后就可以执行方法了。需要注意的是匹配方法时,根据@RequestMapping里面的value路径来匹配的,如果匹配到的有多个,如你配置了通配符,也配置了精确配置,他都会匹配到放在一个集合中,根据规则排序,然后取集合的第一个元素。有兴趣的可以看看这个排序的规则,理论上肯定是路径越精确的会优先,具体代码实现如下:

    protected HandlerMethod lookupHandlerMethod(String lookupPath,HttpServletRequest request)  Exception {
        List<Match> matches = new ArrayList<Match>();

        List<T> directPathMatches = this.urlMap.get(lookupPath);
        if (directPathMatches != ) {
            addMatchingMappings(directPathMatches,matches,request);
        }

         (matches.isEmpty()) {
             No choice but to go through all mappings
            addMatchingMappings(.handlerMethods.keySet(),1)">matches.isEmpty()) {
       //排序规则 Comparator
<Match> comparator = new MatchComparator(getMappingComparator(request));
//进行排序 Collections.sort(matches,comparator);
(logger.isTraceEnabled()) { logger.trace("Found " + matches.size() + " matching mapping(s) for [" + lookupPath + "] : " + matches); } Match bestMatch = matches.get(0); if (matches.size() > 1) { Match secondBestMatch = matches.get(1); if (comparator.compare(bestMatch,secondBestMatch) == 0) { Method m1 = bestMatch.handlerMethod.getmethod(); Method m2 = secondBestMatch.handlerMethod.getmethod(); throw IllegalStateException( "Ambiguous handler methods mapped for HTTP path '" + request.getRequestURL() + "': {" + m1 + "," + m2 + "}"); } } handleMatch(bestMatch.mapping,lookupPath,request); bestMatch.handlerMethod; } else handleNoMatch(handlerMethods.keySet(),request); } }

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐