当前位置:网站首页>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 ~
边栏推荐
- 【每周一坑】信息加密 +【解答】正整数分解质因数
- Force deduction brush question - 98 Validate binary search tree
- I've seen many tutorials, but I still can't write a program well. How can I break it?
- 为什么新手在编程社区提问经常得不到回答,甚至还会被嘲讽?
- Application layer of tcp/ip protocol cluster
- Function optimization and arrow function of ES6
- Number of schemes from the upper left corner to the lower right corner of the chessboard (2)
- Common doubts about the introduction of APS by enterprises
- 1_ Introduction to go language
- 15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
猜你喜欢
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
OLED屏幕的使用
Web security - payload
2022 portal crane driver registration examination and portal crane driver examination materials
Application layer of tcp/ip protocol cluster
使用.Net驱动Jetson Nano的OLED显示屏
Spark SQL chasing Wife Series (initial understanding)
Leetcode question 283 Move zero
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
随机推荐
C language games - minesweeping
2022 nurse (primary) examination questions and new nurse (primary) examination questions
#yyds干货盘点#重新梳理箭头函数的this
1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
JS implementation force deduction 71 question simplified path
Web开发小妙招:巧用ThreadLocal规避层层传值
Leetcode question 448 Find all missing numbers in the array
Boder radius has four values, and boder radius exceeds four values
PG基础篇--逻辑结构管理(事务)
逻辑是个好东西
Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
How does kubernetes support stateful applications through statefulset? (07)
动态切换数据源
Common doubts about the introduction of APS by enterprises
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,
Why do novices often fail to answer questions in the programming community, and even get ridiculed?
Dynamically switch data sources
[wechat applet] operation mechanism and update mechanism
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
OLED屏幕的使用