当前位置:网站首页>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 ~
边栏推荐
- How does kubernetes support stateful applications through statefulset? (07)
- #yyds干货盘点#重新梳理箭头函数的this
- 2022 nurse (primary) examination questions and new nurse (primary) examination questions
- [diy] self designed Microsoft makecode arcade, official open source software and hardware
- What is the difference between procedural SQL and C language in defining variables
- 全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎
- How to upgrade high value-added links in the textile and clothing industry? APS to help
- 看过很多教程,却依然写不好一个程序,怎么破?
- Solution to the 38th weekly match of acwing
- [DSP] [Part 1] start DSP learning
猜你喜欢

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

Infrared thermometer based on STM32 single chip microcomputer (with face detection)
![[diy] how to make a personalized radio](/img/fc/a371322258131d1dc617ce18490baf.jpg)
[diy] how to make a personalized radio

Core principles of video games

(work record) March 11, 2020 to March 15, 2021

性能测试过程和计划

【每周一坑】输出三角形

Comment faire une radio personnalisée

【微信小程序】运行机制和更新机制
![[weekly pit] information encryption + [answer] positive integer factorization prime factor](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[weekly pit] information encryption + [answer] positive integer factorization prime factor
随机推荐
Value of APS application in food industry
use. Net drives the OLED display of Jetson nano
What key progress has been made in deep learning in 2021?
Function optimization and arrow function of ES6
Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace
[DSP] [Part 1] start DSP learning
Design your security architecture OKR
Pycharm remote execution
【DSP】【第二篇】了解C6678和创建工程
C language games - three chess
Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
Web开发小妙招:巧用ThreadLocal规避层层传值
设计你的安全架构OKR
OSPF多区域配置
Mtcnn face detection
[DIY]自己设计微软MakeCode街机,官方开源软硬件
2110 summary of knowledge points and common problems in redis class
Leetcode hot topic Hot 100 day 32: "minimum coverage substring"
使用.Net驱动Jetson Nano的OLED显示屏