当前位置:网站首页>Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
2022-07-06 20:49:00 【51CTO】
Abstract : We can process every request , No need to Controller as well as Service The method in passes values layer by layer , You only need to take the value directly through the local variable .
This article is shared from Huawei cloud community 《 Skillfully used in interceptors ThreadLocal Avoid passing values layer by layer 》, author :KevinQ.
Web A little trouble in development
lately , To be exact , It's always a little trouble : Function layer by layer transfer . What does that mean ? For example, a common requirement description is : Record the details of a user's operation .
In the Java Open source framework of jfinal in , Add a user's interface as an example , Yes :
In order to record the specific operation content and error information added by the user , This record of user actions may need to penetrate good layer methods .
This example may not be very appropriate , But I believe you can understand where the trouble lies .
This parameter is not required for each method in the function call chain , It may just be to pass this parameter to the next called function .
So is there a way to help us not need to pass layer by layer , So as to obtain the method of interface request parameters .
reflection
This is a bit like a global variable , But this variable changes for each request , So is there a way for us to save such a : Global variables for each request Well ?
I've been learning recently Shiro In the process of , And the process of learning ruoyi open source framework , We found that they all use a powerful Java class :ThreadLocal.
shiro Use ThreadLocal Is used to save the current login object , If in the frame , The paging plug-in used PageUtil Use ThreadLocal Save... In the request parameters pageNum And pageSize Equal page parameters . So can we also use ThreadLocal To achieve the same purpose .
Use in interceptor ThreadLocal Staging request parameters
So , Let's try , adopt ThreadLocal Save request parameters , Through the interceptor, we can intercept every request , The following is the implementation method :
We go through ThreadLocal Staging the entire request , Of course , To save memory , You can also save only common parameters that are frequently used , For example, the information of the current login person, etc .
Using parameter
When using , We only need to take the value of the thread local variable :
such , We can process each request , No need to Controller as well as Service The method in passes values layer by layer , You only need to take the value directly through the local variable .
Click to follow , The first time to learn about Huawei's new cloud technology ~
边栏推荐
- 小孩子学什么编程?
- 【OpenCV 例程200篇】220.对图像进行马赛克处理
- Review questions of anatomy and physiology · VIII blood system
- 15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
- 【微信小程序】運行機制和更新機制
- Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
- 【每周一坑】信息加密 +【解答】正整数分解质因数
- 2022 portal crane driver registration examination and portal crane driver examination materials
- 基于STM32单片机设计的红外测温仪(带人脸检测)
- Solution to the 38th weekly match of acwing
猜你喜欢

Learn to punch in Web

15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient

Error analysis ~csdn rebound shell error

强化学习-学习笔记5 | AlphaGo

使用.Net驱动Jetson Nano的OLED显示屏

全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎

拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条

【DSP】【第一篇】开始DSP学习

1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
![[weekly pit] output triangle](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[weekly pit] output triangle
随机推荐
2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
Number of schemes from the upper left corner to the lower right corner of the chessboard (2)
【DSP】【第二篇】了解C6678和创建工程
Utilisation de l'écran OLED
Pytest (3) - Test naming rules
Event center parameter transfer, peer component value transfer method, brother component value transfer
[DSP] [Part 1] start DSP learning
知识图谱之实体对齐二
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
【OpenCV 例程200篇】220.对图像进行马赛克处理
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
What programming do children learn?
OAI 5g nr+usrp b210 installation and construction
C language games - three chess
2110 summary of knowledge points and common problems in redis class
Gui Gui programming (XIII) - event handling
“罚点球”小游戏
1_ Introduction to go language
[diy] how to make a personalized radio
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]