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做的事情是一样的:设置和重置
- 获取以前的值
- 设置新值
new SimpleLocaleContext(request.getLocale())
和new ServletRequestAttributes(request, response)
- 设置回原来的值
这里我们产生了一些问题
-
previousLocaleContext
和previousAttributes
从哪里来的? - 为什么把ContextHolder中的对象换成新的,doService后又要重置ContextHolder?
-
requestAttributes.requestCompleted()
做了些什么? - 为什么ContextHolder要用ThreadLocal?为什么有NamedThreadLocal和NamedInheritableThreadLocal,它们有啥区别?
//todo