美文网首页
源码跟踪-springmvc(二):LocaleContextH

源码跟踪-springmvc(二):LocaleContextH

作者: A_19cb | 来源:发表于2018-12-23 17:54 被阅读0次

    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

    相关文章

      网友评论

          本文标题:源码跟踪-springmvc(二):LocaleContextH

          本文链接:https://www.haomeiwen.com/subject/ztinkqtx.html