源码跟踪-springmvc(二):LocaleContextH

2018-12-23  本文已影响0人  A_19cb

FrameworkServlet.processRequest(删减版)

protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // (1)
        LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
        LocaleContext localeContext = buildLocaleContext(request);

        RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);

        // (2)
        initContextHolders(request, localeContext, requestAttributes);

            doService(request, response);

            // (3)
            resetContextHolders(request, previousLocaleContext, previousAttributes);
            if (requestAttributes != null) {
                requestAttributes.requestCompleted();
            }
    }

两个ContextHolder做的事情是一样的:设置和重置

  1. 获取以前的值
  2. 设置新值
    new SimpleLocaleContext(request.getLocale())new ServletRequestAttributes(request, response)
  3. 设置回原来的值

这里我们产生了一些问题

  1. previousLocaleContextpreviousAttributes从哪里来的?
  2. 为什么把ContextHolder中的对象换成新的,doService后又要重置ContextHolder?
  3. requestAttributes.requestCompleted()做了些什么?
  4. 为什么ContextHolder要用ThreadLocal?为什么有NamedThreadLocal和NamedInheritableThreadLocal,它们有啥区别?
    //todo
上一篇下一篇

猜你喜欢

热点阅读