关于springboot:spring-boot-http接口单元测试运行流程分析


HandlerMappingIntrospector的initHandlerMappings()具体逻辑如下:

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


能够看到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);

其中mappedHandler = getHandler(processedRequest);获取HandlerExecutionChain。

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

在下面codwe块中获取适合的HandlerAdapter。

中依据mappedHandler失去HandlerMethod

中根据HandlerMethod利用反射技术去调用指标类,取得返回值。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理