当前位置: 首页 > wzjs >正文

第一简历模板网搜索引擎优化

第一简历模板网,搜索引擎优化,网站制作学校找哪家,怎么做微信小说网站1. 核心作用 RequestContextHolder 是 Spring 框架中用于管理当前线程请求上下文的工具类。它的主要功能包括: 存储请求上下文:通过 ThreadLocal 存储与当前线程绑定的 RequestAttributes 对象(如 ServletRequestAttributes)。 全…

1. 核心作用


RequestContextHolder 是 Spring 框架中用于管理当前线程请求上下文的工具类。它的主要功能包括:
存储请求上下文:通过 ThreadLocal 存储与当前线程绑定的 RequestAttributes 对象(如 ServletRequestAttributes)。
全局访问点:提供静态方法,允许任意层代码(如 Service 或 DAO 层)无需显式传递 HttpServletRequest 即可获取当前请求信息。
 


2. 核心字段

private static final ThreadLocal<RequestAttributes> requestAttributesHolder =new NamedThreadLocal<>("Request attributes");private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =new NamedInheritableThreadLocal<>("Request context");
  • requestAttributesHolder:

使用普通的 ThreadLocal,确保每个线程都有独立的 RequestAttributes 实例。
默认情况下,子线程无法访问父线程的 ThreadLocal 数据。

  • inheritableRequestAttributesHolder:

使用 InheritableThreadLocal,允许子线程继承父线程的 RequestAttributes。
适用于异步任务或需要跨线程传递请求上下文的场景。

3. 核心方法


(1) 设置请求属性

public static void setRequestAttributes(@Nullable RequestAttributes attributes, boolean inheritable) {if (attributes == null) {resetRequestAttributes();} else {if (inheritable) {inheritableRequestAttributesHolder.set(attributes);requestAttributesHolder.remove();} else {requestAttributesHolder.set(attributes);inheritableRequestAttributesHolder.remove();}}
}

逻辑说明:
如果 attributes 为 null,调用 resetRequestAttributes() 清空当前线程的请求上下文。
根据 inheritable 参数决定将 attributes 存入普通 ThreadLocal 或可继承的 ThreadLocal。
确保两个 ThreadLocal 不会同时持有数据,避免冲突。

(2) 获取请求属性

@Nullable
public static RequestAttributes getRequestAttributes() {RequestAttributes attributes = requestAttributesHolder.get();if (attributes == null) {attributes = inheritableRequestAttributesHolder.get();}return attributes;
}

逻辑说明:
首先尝试从普通 ThreadLocal 中获取 RequestAttributes。
如果未找到,则尝试从可继承的 ThreadLocal 中获取。


(3) 清理请求属性

public static void resetRequestAttributes() {requestAttributesHolder.remove();inheritableRequestAttributesHolder.remove();
}

逻辑说明:
清空当前线程的 ThreadLocal 数据,防止内存泄漏。
 

4. 设计思想


(1) 线程隔离

使用 ThreadLocal 实现线程级别的数据隔离,确保不同线程之间的请求上下文互不干扰。
在 Web 应用中,每个 HTTP 请求都会分配一个独立的线程,RequestContextHolder 利用这一特性来管理请求上下文。


(2) 全局访问

提供静态方法(如 getRequestAttributes()),让任意层代码都能方便地获取当前请求上下文。
解耦了业务代码与 Servlet API,提升代码的可测试性和灵活性。

(3) 异步支持

通过 InheritableThreadLocal 支持子线程继承父线程的请求上下文,适应异步任务场景。

 
5. 使用场景

(1) 获取当前请求对象

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {HttpServletRequest request = attributes.getRequest();String paramValue = request.getParameter("paramName");
}

 在非 Controller 层(如 Service 层)中获取当前请求的参数或 Header。


(2) 异步任务中传递请求上下文

RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
CompletableFuture.runAsync(() -> {RequestContextHolder.setRequestAttributes(attributes);// 异步任务中可以继续使用请求上下文
});

手动将父线程的 RequestAttributes 传递到子线程中。

6 Springboot设置/清除RequestContextHolder的时机

设置 RequestContextHolder 的时机

在请求进入时自动设置
Spring MVC 框架会在每次 HTTP 请求到达时,自动将当前请求的 HttpServletRequest 和 HttpServletResponse 绑定到 RequestContextHolder。
这是由 DispatcherServlet 完成的,通常不需要手动干预。
具体实现位于 org.springframework.web.context.request.RequestContextListener 或 org.springframework.web.filter.RequestContextFilter 中。
 

清除 RequestContextHolder 的时机

在请求结束时自动清除
Spring MVC 会在请求处理完成后,自动清除 RequestContextHolder 中的上下文信息。
这是通过 RequestContextListener 或 RequestContextFilter 在请求生命周期结束时完成的。

7. 注意事项

内存泄漏:

ThreadLocal 的生命周期与线程绑定,如果线程池中的线程被复用而未清理 ThreadLocal,可能导致内存泄漏。
Spring 的 RequestContextListener 或 DispatcherServlet 会在请求结束时自动清理 ThreadLocal。

异步任务:

默认情况下,异步任务不会继承父线程的 ThreadLocal 数据,需手动传递或启用 InheritableThreadLocal。

多线程环境:

在多线程环境中使用时,需特别注意请求上下文的传递和清理。

8. 总结

RequestContextHolder 是 Spring 框架中用于管理请求上下文的核心工具类,其设计充分利用了 ThreadLocal 的线程隔离特性,提供了简单易用的 API 来访问当前线程的请求上下文。通过合理的使用和清理,可以有效提升代码的灵活性和可维护性。


文章转载自:

http://0uztfoyU.qbksx.cn
http://P03MRk2V.qbksx.cn
http://LjhTk1Wy.qbksx.cn
http://tM9Yu3eQ.qbksx.cn
http://sW5NmBXY.qbksx.cn
http://77UyuwPS.qbksx.cn
http://tWzcMScr.qbksx.cn
http://6RDpZdc6.qbksx.cn
http://2VfodE8o.qbksx.cn
http://C3LIRgHQ.qbksx.cn
http://mwxntlE1.qbksx.cn
http://USSrE00V.qbksx.cn
http://fZfFvoGa.qbksx.cn
http://yzO1yafw.qbksx.cn
http://4vREy9up.qbksx.cn
http://dLv3LnjF.qbksx.cn
http://L68mDruz.qbksx.cn
http://dg9osOrR.qbksx.cn
http://RgiE8Wzw.qbksx.cn
http://8J0sm5n7.qbksx.cn
http://IDpCMqAH.qbksx.cn
http://nBFrks8V.qbksx.cn
http://8Md47f5S.qbksx.cn
http://Z2OekRDa.qbksx.cn
http://6dxLmXlI.qbksx.cn
http://Bq1DsvJr.qbksx.cn
http://iPcNEXgB.qbksx.cn
http://pJlJ2ICL.qbksx.cn
http://WISbHa5n.qbksx.cn
http://UU7MsRgZ.qbksx.cn
http://www.dtcms.com/wzjs/751992.html

相关文章:

  • 淘宝网站建设的目标seo优化排名易下拉技巧
  • 一个ip做几个网站吗动易会提示模版文件"默认网站首页问答模板.html"找不到
  • 用织梦做网站都需要用什么网站建设方案书腾讯云
  • 现在怎么建设一个网站外贸行业网络推广
  • 怎么做网站转盘易迈互联网站建设怎么样
  • 济南市网站建设怎么弄网站做网站卖东西
  • 企业网站建设基本要素北京平面设计网站
  • 网站无法被百度收录三门峡市住房建设局网站
  • 对于网站建设的体会广州开发小程序
  • 17网站一起做网批如何制作互联网网站
  • 福建做网站公司小型门户网站有哪些
  • 怎么做一个免费的网站营销方案
  • 健身器械网站建设案例互联网营销师报名
  • 衡水企业网站制作深圳门户网站开发
  • 不干净的网站做性玉树电子商务网站建设
  • wap网站代码十堰学校网站建设
  • 可信赖的赣州网站建设怎么查看域名网站的容量到期
  • 调颜色网站美食地图网站开发
  • 小城镇建设的网站文献wordpress 登录机制
  • 站长网网站模板耒阳市古雍网站建设店
  • 电商网站开发服务团建智慧登陆官网
  • 张家港网站网络公司网页特效精灵
  • 捷克cz公司网站wordpress 主题预览
  • 南宁网站建设费用建筑招聘信息最新招聘2022
  • 选择seo网站排名优化可以做一键拨号和导航的网站
  • 展示型外贸网站建设郑州网站开发技术
  • 天津品牌网站建设wordpress百度插件
  • 建设部网站 注册违规找人做app网站
  • 长春做网站哪个公司好网站语言切换功能如何做
  • 做一样的模板网站会被告侵权吗深圳商业网站建设案例