当前位置:网站首页>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 ~
边栏推荐
- 强化学习-学习笔记5 | AlphaGo
- [DIY]如何制作一款個性的收音機
- 基于STM32单片机设计的红外测温仪(带人脸检测)
- [diy] self designed Microsoft makecode arcade, official open source software and hardware
- 7. Data permission annotation
- 知识图谱之实体对齐二
- 【微信小程序】運行機制和更新機制
- 正则表达式收集
- Summary of different configurations of PHP Xdebug 3 and xdebug2
- 新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
猜你喜欢
Event center parameter transfer, peer component value transfer method, brother component value transfer
How to upgrade high value-added links in the textile and clothing industry? APS to help
Utilisation de l'écran OLED
性能测试过程和计划
Discussion on beegfs high availability mode
Core principles of video games
Logic is a good thing
Implementation of packaging video into MP4 format and storing it in TF Card
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
Pytest (3) - Test naming rules
随机推荐
JMeter server resource indicator monitoring (CPU, memory, etc.)
Yyds dry goods count re comb this of arrow function
Variable star --- article module (1)
【微信小程序】運行機制和更新機制
[weekly pit] output triangle
全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎
SSO single sign on
Quel genre de programmation les enfants apprennent - ils?
过程化sql在定义变量上与c语言中的变量定义有什么区别
"Penalty kick" games
Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,
【每周一坑】输出三角形
How to turn a multi digit number into a digital list
看过很多教程,却依然写不好一个程序,怎么破?
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
Use of OLED screen
[asp.net core] set the format of Web API response data -- formatfilter feature
OSPF多区域配置
[DIY]如何制作一款個性的收音機
[wechat applet] operation mechanism and update mechanism