美文网首页
SpringMVC之RequestContextHolder分析

SpringMVC之RequestContextHolder分析

作者: gotchar | 来源:发表于2019-10-25 00:09 被阅读0次

    转自:https://zhuanlan.zhihu.com/p/24293062?refer=dreawer

    1.RequestContextHolder的使用

    RequestContextHolder顾名思义,持有上下文的Request容器.使用是很简单的,具体使用如下:

    //两个方法在没有使用JSF的项目中是没有区别的RequestAttributesrequestAttributes=RequestContextHolder.currentRequestAttributes();//                                            RequestContextHolder.getRequestAttributes();//从session里面获取对应的值Stringstr=(String)requestAttributes.getAttribute("name",RequestAttributes.SCOPE_SESSION);HttpServletRequestrequest=((ServletRequestAttributes)requestAttributes).getRequest();HttpServletResponseresponse=((ServletRequestAttributes)requestAttributes).getResponse();

    看到这一般都会想到几个问题:

    request和response怎么和当前请求挂钩?

    request和response等是什么时候设置进去的?

    2.解决疑问

    2.1 request和response怎么和当前请求挂钩?

    首先分析RequestContextHolder这个类,里面有两个ThreadLocal保存当前线程下的request,关于ThreadLocal可以参考我的另一篇博文[Java学习记录--ThreadLocal使用案例]

    //得到存储进去的requestprivatestaticfinalThreadLocal<RequestAttributes>requestAttributesHolder=newNamedThreadLocal<RequestAttributes>("Request attributes");//可被子线程继承的requestprivatestaticfinalThreadLocal<RequestAttributes>inheritableRequestAttributesHolder=newNamedInheritableThreadLocal<RequestAttributes>("Request context");

    再看`getRequestAttributes()`方法,相当于直接获取ThreadLocal里面的值,这样就保证了每一次获取到的Request是该请求的request.

    publicstaticRequestAttributesgetRequestAttributes(){RequestAttributesattributes=requestAttributesHolder.get();if(attributes==null){attributes=inheritableRequestAttributesHolder.get();}returnattributes;}

    2.2request和response等是什么时候设置进去的?

    找这个的话需要对springMVC结构的`DispatcherServlet`的结构有一定了解才能准确的定位该去哪里找相关代码.

    在IDEA中会显示如下的继承关系.

    左边1这里是Servlet的接口和实现类.

    右边2这里是使得SpringMVC具有Spring的一些环境变量和Spring容器.类似的XXXAware接口就是对该类提供Spring感知,简单来说就是如果想使用Spring的XXXX就要实现XXXAware,spring会把需要的东西传送过来.

    那么剩下要分析的的就是三个类,简单看下源码

    1. HttpServletBean 进行初始化工作

    2. FrameworkServlet 初始化 WebApplicationContext,并提供service方法预处理请

    3. DispatcherServlet 具体分发处理.

    那么就可以在FrameworkServlet查看到该类重写了service(),doGet(),doPost()...等方法,这些实现里面都有一个预处理方法`processRequest(request, response);`,所以定位到了我们要找的位置

    查看`processRequest(request, response);`的实现,具体可以分为三步:

    获取上一个请求的参数

    重新建立新的参数

    设置到XXContextHolder

    父类的service()处理请求

    恢复request

    发布事件

    protectedfinalvoidprocessRequest(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{longstartTime=System.currentTimeMillis();ThrowablefailureCause=null;//获取上一个请求保存的LocaleContextLocaleContextpreviousLocaleContext=LocaleContextHolder.getLocaleContext();//建立新的LocaleContextLocaleContextlocaleContext=buildLocaleContext(request);//获取上一个请求保存的RequestAttributesRequestAttributespreviousAttributes=RequestContextHolder.getRequestAttributes();//建立新的RequestAttributesServletRequestAttributesrequestAttributes=buildRequestAttributes(request,response,previousAttributes);WebAsyncManagerasyncManager=WebAsyncUtils.getAsyncManager(request);asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(),newRequestBindingInterceptor());//具体设置的方法initContextHolders(request,localeContext,requestAttributes);try{doService(request,response);}catch(ServletExceptionex){failureCause=ex;throwex;}catch(IOExceptionex){failureCause=ex;throwex;}catch(Throwableex){failureCause=ex;thrownewNestedServletException("Request processing failed",ex);}finally{//恢复resetContextHolders(request,previousLocaleContext,previousAttributes);if(requestAttributes!=null){requestAttributes.requestCompleted();}if(logger.isDebugEnabled()){if(failureCause!=null){this.logger.debug("Could not complete request",failureCause);}else{if(asyncManager.isConcurrentHandlingStarted()){logger.debug("Leaving response open for concurrent processing");}else{this.logger.debug("Successfully completed request");}}}//发布事件publishRequestHandledEvent(request,response,startTime,failureCause);}}

    再看initContextHolders(request, localeContext, requestAttributes)方法,把新的RequestAttributes设置进LocalThread,实际上保存的类型为ServletRequestAttributes,这也是为什么在使用的时候可以把RequestAttributes强转为ServletRequestAttributes.

    privatevoidinitContextHolders(HttpServletRequestrequest,LocaleContextlocaleContext,RequestAttributesrequestAttributes){if(localeContext!=null){LocaleContextHolder.setLocaleContext(localeContext,this.threadContextInheritable);}if(requestAttributes!=null){RequestContextHolder.setRequestAttributes(requestAttributes,this.threadContextInheritable);}if(logger.isTraceEnabled()){logger.trace("Bound request context to thread: "+request);}}

    因此RequestContextHolder里面最终保存的为ServletRequestAttributes,这个类相比`RequestAttributes`方法是多了很多.

    相关文章

      网友评论

          本文标题:SpringMVC之RequestContextHolder分析

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