当前位置:网站首页>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 ~
边栏推荐
- C language operators
- 2022 Guangdong Provincial Safety Officer C certificate third batch (full-time safety production management personnel) simulation examination and Guangdong Provincial Safety Officer C certificate third
- OAI 5G NR+USRP B210安装搭建
- OSPF多区域配置
- In unity space, an object moves around a fixed point on the sphere at a fixed speed
- Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
- Unity load AB package
- Catch ball game 1
- 解剖生理学复习题·VIII血液系统
- PHP online examination system version 4.0 source code computer + mobile terminal
猜你喜欢
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
“罚点球”小游戏
1_ Introduction to go language
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,
[diy] how to make a personalized radio
SSO single sign on
1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
[weekly pit] information encryption + [answer] positive integer factorization prime factor
Event center parameter transfer, peer component value transfer method, brother component value transfer
随机推荐
Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎
How to upgrade high value-added links in the textile and clothing industry? APS to help
Simple continuous viewing PTA
[diy] self designed Microsoft makecode arcade, official open source software and hardware
Solution to the 38th weekly match of acwing
[weekly pit] information encryption + [answer] positive integer factorization prime factor
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
Learn to punch in Web
Web开发小妙招:巧用ThreadLocal规避层层传值
【微信小程序】運行機制和更新機制
为什么新手在编程社区提问经常得不到回答,甚至还会被嘲讽?
【DSP】【第二篇】了解C6678和创建工程
什么是RDB和AOF
看过很多教程,却依然写不好一个程序,怎么破?
What programming do children learn?
Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
Value of APS application in food industry
知识图谱构建流程步骤详解
Leetcode question 283 Move zero