SpringMVC之RequestContextHolder分析

最近遇到的问题是在service获取request和response,正常来说在service层是没有request的,然而直接从controlller传过来的话解决方法太粗暴,后来发现了SpringMVC提供的RequestContextHolder遂去分析一番,并借此对SpringMVC的结构深入了解一下,后面会再发文章详细分析源码

 

1.RequestContextHolder的使用

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

 
  1. //两个方法在没有使用JSF的项目中是没有区别的

  2. RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();

  3. // RequestContextHolder.getRequestAttributes();

  4. //从session里面获取对应的值

  5. String str = (String) requestAttributes.getAttribute("name",RequestAttributes.SCOPE_SESSION);

  6.  
  7. HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();

  8. HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();

 

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

  1. request和response怎么和当前请求挂钩?
  2. request和response等是什么时候设置进去的?

2.解决疑问

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

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

 
  1. //得到存储进去的request

  2. private static final ThreadLocal requestAttributesHolder =

  3. new NamedThreadLocal("Request attributes");

  4. //可被子线程继承的request

  5. private static final ThreadLocal inheritableRequestAttributesHolder =

  6. new NamedInheritableThreadLocal("Request context");

 

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

 
  1. public static RequestAttributes getRequestAttributes() {

  2. RequestAttributes attributes = requestAttributesHolder.get();

  3. if (attributes == null) {

  4. attributes = inheritableRequestAttributesHolder.get();

  5. }

  6. return attributes;

  7. }

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);`的实现,具体可以分为三步:

  1. 获取上一个请求的参数
  2. 重新建立新的参数
  3. 设置到XXContextHolder
  4. 父类的service()处理请求
  5. 恢复request
  6. 发布事件

 

 
  1. protected final void processRequest(HttpServletRequest request, HttpServletResponse response)

  2. throws ServletException, IOException {

  3. long startTime = System.currentTimeMillis();

  4. Throwable failureCause = null;

  5. //获取上一个请求保存的LocaleContext

  6. LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();

  7. //建立新的LocaleContext

  8. LocaleContext localeContext = buildLocaleContext(request);

  9. //获取上一个请求保存的RequestAttributes

  10. RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();

  11. //建立新的RequestAttributes

  12. ServletRequestAttributes requestAttributes = buildRequestAttributes(request,

  13. response, previousAttributes);

  14. WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

  15. asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(),

  16. new RequestBindingInterceptor());

  17. //具体设置的方法

  18. initContextHolders(request, localeContext, requestAttributes);

  19. try {

  20. doService(request, response);

  21. }

  22. catch (ServletException ex) {

  23. failureCause = ex;

  24. throw ex;

  25. }

  26. catch (IOException ex) {

  27. failureCause = ex;

  28. throw ex;

  29. }

  30. catch (Throwable ex) {

  31. failureCause = ex;

  32. throw new NestedServletException("Request processing failed", ex);

  33. }

  34. finally {

  35. //恢复

  36. resetContextHolders(request, previousLocaleContext, previousAttributes);

  37. if (requestAttributes != null) {

  38. requestAttributes.requestCompleted();

  39. }

  40. if (logger.isDebugEnabled()) {

  41. if (failureCause != null) {

  42. this.logger.debug("Could not complete request", failureCause);

  43. }

  44. else {

  45. if (asyncManager.isConcurrentHandlingStarted()) {

  46. logger.debug("Leaving response open for concurrent processing");

  47. }

  48. else {

  49. this.logger.debug("Successfully completed request");

  50. }

  51. }

  52. }

  53. //发布事件

  54. publishRequestHandledEvent(request, response, startTime, failureCause);

  55. }

  56. }

 

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

 

 
  1. private void initContextHolders(HttpServletRequest request,

  2. LocaleContext localeContext,

  3. RequestAttributes requestAttributes) {

  4. if (localeContext != null) {

  5. LocaleContextHolder.setLocaleContext(localeContext,

  6. this.threadContextInheritable);

  7. }

  8. if (requestAttributes != null) {

  9. RequestContextHolder.setRequestAttributes(requestAttributes,

  10. this.threadContextInheritable);

  11. }

  12. if (logger.isTraceEnabled()) {

  13. logger.trace("Bound request context to thread: " + request);

  14. }

  15. }

 

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

转:https://blog.csdn.net/zzy7075/article/details/53559902/

你可能感兴趣的:(框架)