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

做网站最简单wordpress为什么慢

做网站最简单,wordpress为什么慢,湖州医院网站建设方案,最难进的十大央企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://www.dtcms.com/a/447397.html

相关文章:

  • 做设计的兼职网站有哪些手机装修设计软件
  • 安徽省建设工程安全 协会网站中企动力网站建设精品案例
  • 如何弄自己的公司网站关于网站建设申请报告
  • 无锡市新吴区建设环保局网站wordpress query_posts
  • 网站建设要备案吗wordpress国产主题
  • Robot Framework7.0 之Listener v3 深度实战
  • 宁波专业的网站建设摄影作品展示网站flash全站源码
  • 亿网行网站建设凤岗镇网站建设
  • 展厅设计素材网站理发店会员管理软件免费
  • 网站内页标题wordpress 4.0 4.6
  • 高端网站欣赏支付宝小程序开发费用
  • 上海网站开发与设计湘潭网站建设 技精磐石网络
  • 青岛模板化网站建设网站链接推广
  • dw自己做网站上海服装外贸公司排名
  • 照明灯具类企业网站网站运营建设
  • 西宁网站建设君博正规wordpress建小说站收费
  • 网站开发时会遇到哪些问题贵州 跨境电商网站建设
  • LangChain4j 新版本的核心升级点 —— @AiService 声明式接口
  • NX511NX512美光固态闪存NX516NX517
  • 重庆五号线金建站wordpress ip无法设置
  • 基于 seajs 的高性能网站开发和优化实践_王保平(淘宝)建个微商城网站
  • 继续打卡hot100
  • 做封面下载网站个人 做自媒体 建网站
  • 旧电脑做网站服务器免费拓客软件
  • wordpress当前版本中山seo网络推广
  • 建设营销型网站不足之处在门户网站上做推广
  • 公司网站一年多少钱网站设计项目建设内容
  • 301的网站用什么来做域名交易域名出售
  • 以实训为载体:养老实训室助力养老服务标准化建设的四大路径
  • 广州哪里有网站建设网站建设的市场分析