当前位置:网站首页>Web开发小妙招:巧用ThreadLocal规避层层传值
Web开发小妙招:巧用ThreadLocal规避层层传值
2022-07-06 12:42:00 【51CTO】
摘要:我们可以在处理每次请求的过程中,无需从Controller以及Service中的方法层层传值,只需要直接通过该局部变量取值即可。
本文分享自华为云社区《 拦截器中巧用ThreadLocal规避层层传值》,作者:KevinQ。
Web开发中的一点麻烦事
最近,准确地说,是一直都有的一点麻烦事:函数层层传递。什么意思呢?比如说有个很常见的需求描述是:记录用户的某次操作明细。
以在Java的开源框架jfinal中,操作添加一个用户的接口为例,有:
为了记录用户添加的具体操作内容以及信息错误,这个记录用户操作的记录可能需要穿透好层方法。
举得这个例子或许不是很恰当,但是相信大家可以理解这个麻烦点在哪里。
函数调用链中的每个方法并不是需要这个参数,而可能仅仅是为了向下个被调用的函数传递这个参数。
那么是否有方法帮助我们来不需要这样逐层传递,从而获取接口请求参数的方法呢。
思考
这有点类似与一个全局变量,但是这个变量对每次请求来说是变化的,那么是否有一种方式能够让我们来保存这样一种:针对每次请求的全局变量呢?
在最近学习Shiro的过程中,以及学习若依开源框架的过程中,我们发现其均使用到了一个强大的Java类:ThreadLocal。
shiro使用ThreadLocal是用来保存当前登录对象,若依框架中,其中所使用的分页插件PageUtil使用ThreadLocal保存请求参数中的pageNum与pageSize等分页参数。因此我们是不是也可以使用ThreadLocal来达到同样的目的。
在拦截器中使用ThreadLocal暂存请求参数
为此,我们来尝试一下,通过ThreadLocal保存请求参数,通过拦截器我们可以拦截每次请求,如下是实现方式:
我们通过ThreadLocal将整个请求暂存起来,当然,为了节约内存,也可以只保存使用频次高的通用参数,比如当前登录人的信息等等。
使用参数
使用时,只需要我们通过这个线程局部变量取值即可:
如此,我们便可以在处理每次请求的过程中,无需从Controller以及Service中的方法层层传值,只需要直接通过该局部变量取值即可。
边栏推荐
- 棋盘左上角到右下角方案数(2)
- Function optimization and arrow function of ES6
- What key progress has been made in deep learning in 2021?
- [diy] how to make a personalized radio
- OAI 5G NR+USRP B210安装搭建
- [weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
- [asp.net core] set the format of Web API response data -- formatfilter feature
- Variable star --- article module (1)
- In line elements are transformed into block level elements, and display transformation and implicit transformation
- Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
猜你喜欢
【微信小程序】运行机制和更新机制
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
Comment faire une radio personnalisée
use. Net drives the OLED display of Jetson nano
Notes on beagleboneblack
1_ Introduction to go language
为什么新手在编程社区提问经常得不到回答,甚至还会被嘲讽?
OAI 5G NR+USRP B210安装搭建
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
随机推荐
Solution to the 38th weekly match of acwing
Maximum likelihood estimation and cross entropy loss
正则表达式收集
Variable star --- article module (1)
[weekly pit] output triangle
小孩子學什麼編程?
(work record) March 11, 2020 to March 15, 2021
Yyds dry goods count re comb this of arrow function
Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
Force deduction brush question - 98 Validate binary search tree
Core principles of video games
use. Net analysis Net talent challenge participation
自定义限流注解
【微信小程序】運行機制和更新機制
(工作记录)2020年3月11日至2021年3月15日
Use of OLED screen
Value of APS application in food industry
逻辑是个好东西
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
PHP online examination system version 4.0 source code computer + mobile terminal