spring boot http接口单元测试运行流程分析

spring boot http接口单元测试运行流程分析_第1张图片

spring boot http接口单元测试运行流程分析_第2张图片

spring boot http接口单元测试运行流程分析_第3张图片

spring boot http接口单元测试运行流程分析_第4张图片

image.png

spring boot http接口单元测试运行流程分析_第5张图片

spring boot http接口单元测试运行流程分析_第6张图片

spring boot http接口单元测试运行流程分析_第7张图片
HandlerMappingIntrospector的initHandlerMappings()具体逻辑如下:

    private static List initHandlerMappings(ApplicationContext applicationContext) {
        Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
                applicationContext, HandlerMapping.class, true, false);
        if (!beans.isEmpty()) {
            List mappings = new ArrayList<>(beans.values());
            AnnotationAwareOrderComparator.sort(mappings);
            return Collections.unmodifiableList(mappings);
        }
        return Collections.unmodifiableList(initFallback(applicationContext));
    }

spring boot http接口单元测试运行流程分析_第8张图片
可以看到beans的数量为8,此时HandlerMappingIntrospector.handlerMappings属性值已经被赋值了。

    private void filterAndRecordMetrics(HttpServletRequest request,
            HttpServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {
        Object handler;
        try {
            handler = getHandler(request);//1
        }
        catch (Exception ex) {
            logger.debug("Unable to time request", ex);
            filterChain.doFilter(request, response);
            return;
        }
        //
        filterAndRecordMetrics(request, response, filterChain, handler);
    }

第1处获取HandlerMethod;第2处,继续执行doFilter()方法,最终在MockFilterChain.doFilter()执行到DispatcherServlet.doDispatch(request, response);
spring boot http接口单元测试运行流程分析_第9张图片
其中mappedHandler = getHandler(processedRequest);获取HandlerExecutionChain。

Determine handler adapter for the current request.
                HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

在上面codwe块中获取合适的HandlerAdapter。
spring boot http接口单元测试运行流程分析_第10张图片
中根据mappedHandler得到HandlerMethod
image.png
中依据HandlerMethod利用反射技术去调用目标类,获得返回值。

你可能感兴趣的:(springboot单元测试)