SpringMvc之RequestContextHolder

SpringMvc之RequestContextHolder

2018年04月21日 22:24:12 小柴林 阅读数:330

项目中除了Controller能够接收到请求对象HttpServletRequest和HttpServletResponse外,如果需要在service层使用以上请求和/或响应对象,如果直接从Controller传递过service层感觉太麻烦,而且耦合度太高;那么有什么方式可以优雅的解决该问题么? SpringMvc提供了RequestContextHolder对象!

一、初识RequestContextHolder

RequestContextHolder顾名思义,持有上下文的Request容器~

1、使用以下2个ThreadLocal对象保存RequestAttributes

 
  1. private static final boolean jsfPresent =

  2. ClassUtils.isPresent("javax.faces.context.FacesContext", RequestContextHolder.class.getClassLoader());

  3.  
  4. private static final ThreadLocal<RequestAttributes> requestAttributesHolder =

  5. new NamedThreadLocal<RequestAttributes>("Request attributes");

  6.  
  7. private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =

  8. new NamedInheritableThreadLocal<RequestAttributes>("Request context");

2、获取RequestAttributes、HttpServletRequest、HttpServletResponse

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

  2.  
  3. RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();

  4.  
  5. //RequestContextHolder.getRequestAttributes();

  6.  
  7. //B1.从request里面获取对应的值

  8.  
  9. String str = (String) requestAttributes.getAttribute("name",RequestAttributes.SCOPE_REQUEST);

  10.  
  11. //B2.从session里面获取对应的值

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

  14.  
  15. //C.获取当前HttpServletRequest、HttpServletResponse

  16.  
  17. HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();

  18.  
  19. HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();

疑问:

1)RequestContextHolder获取request和response是怎么和当前请求挂钩的?

2)request和response是什么时候设置进RequestContextHolder?

 

二、疑问解答

1)RequestContextHolder获取request和response是怎么和当前请求挂钩的?

RequestContextHolder里面有两个ThreadLocal成员属性保存当前线程下的RequestAttributes.

详见以上第一点!

2)request和response是什么时候设置进RequestContextHolder?

HttpServlet继承关系结构:

 

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

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

1. HttpServletBean 进行初始化工作

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

3. DispatcherServlet 具体分发处理

 

那么就可以在FrameworkServlet查看到该类重写了service(),doGet(),doPost()...等方法

 
  1. /**

  2. * Override the parent class implementation in order to intercept PATCH requests.

  3. */

  4. @Override

  5. protected void service(HttpServletRequest request, HttpServletResponse response)

  6. throws ServletException, IOException {

  7.  
  8. if (HttpMethod.PATCH.matches(request.getMethod())) {

  9. processRequest(request, response);

  10. }

  11. else {

  12. super.service(request, response);

  13. }

  14. }

这些实现里面都有一个预处理方法processRequest(request, response),所以定位到了我们要找的位置

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

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

  2. * Process this request, publishing an event regardless of the outcome.

  3. * <p>The actual event handling is performed by the abstract

  4. * {@link #doService} template method.

  5. */

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

  7. throws ServletException, IOException {

  8.  
  9. long startTime = System.currentTimeMillis();

  10. Throwable failureCause = null;

  11. //获取上一次请求保存的LocalContext

  12. LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();

  13.  
  14. LocaleContext localeContext = buildLocaleContext(request); //创建新的LocalContext

  15.  
  16. //获取上一次保存的RequestAtttibutes

  17. RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();

  18. //创建新的RequestAttributes

  19. ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);

  20.  
  21. WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

  22. asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());

  23.  
  24. //初始化方法

  25. initContextHolders(request, localeContext, requestAttributes);

  26.  
  27. try {

  28. doService(request, response);

  29. }

  30. catch (ServletException ex) {

  31. failureCause = ex;

  32. throw ex;

  33. }

  34. catch (IOException ex) {

  35. failureCause = ex;

  36. throw ex;

  37. }

  38. catch (Throwable ex) {

  39. failureCause = ex;

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

  41. }

  42.  
  43. finally {

  44. //恢复本次请求

  45. resetContextHolders(request, previousLocaleContext, previousAttributes);

  46. if (requestAttributes != null) {

  47. requestAttributes.requestCompleted();

  48. }

  49.  
  50. if (logger.isDebugEnabled()) {

  51. if (failureCause != null) {

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

  53. }

  54. else {

  55. if (asyncManager.isConcurrentHandlingStarted()) {

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

  57. }

  58. else {

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

  60. }

  61. }

  62. }

  63. //事件发布

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

  65. }

  66. }

再看创建RequestAttributes的方法buildRequestAttributes

 
  1. /**

  2. * Build ServletRequestAttributes for the given request (potentially also

  3. * holding a reference to the response), taking pre-bound attributes

  4. * (and their type) into consideration.

  5. * @param request current HTTP request

  6. * @param response current HTTP response

  7. * @param previousAttributes pre-bound RequestAttributes instance, if any

  8. * @return the ServletRequestAttributes to bind, or {@code null} to preserve

  9. * the previously bound instance (or not binding any, if none bound before)

  10. * @see RequestContextHolder#setRequestAttributes

  11. */

  12. protected ServletRequestAttributes buildRequestAttributes(

  13. HttpServletRequest request, HttpServletResponse response, RequestAttributes previousAttributes) {

  14.  
  15. if (previousAttributes == null || previousAttributes instanceof ServletRequestAttributes) {

  16. return new ServletRequestAttributes(request, response);

  17. }

  18. else {

  19. return null; // preserve the pre-bound RequestAttributes instance

  20. }

  21. }

把新的RequestAttributes设置进LocalThread,实际上保存的类型为ServletRequestAttributes,这也是为什么在使用的时候可以把RequestAttributes强转为ServletRequestAttributes。

initContextHolders重新设置LocalContextHolder和RequestContextHolder对象成员属性

 
  1. private void initContextHolders(

  2. HttpServletRequest request, LocaleContext localeContext, RequestAttributes requestAttributes) {

  3.  
  4. if (localeContext != null) {

  5. LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable);

  6. }

  7. if (requestAttributes != null) {

  8. RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);

  9. }

  10. if (logger.isTraceEnabled()) {

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

  12. }

  13. }

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值