当前位置:网站首页>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 21:25:00 【Huawei cloud developer Alliance】
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 :
public class XXController() { public void addUser() { // Get operator Integer opUserId = Integer.parseInt(getHeader("opUserId")); // Get other parameters ... service.addUser(...., opUserId); renderAppMsg(" Add user successfully "); }}
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 .
public class XXService() { public void addUser(String tel, String name, String password, Integer opUserId) { checkTel(tel, opUserId); checkName(name, opUserId); checkPassword(password, opUserId); } public void checkTel(String tel, Integer opUserId) { check(tel, opUserId); } publc void check(..., Integer opUserId) { // ... }}
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 :
package com.holdoa.core.interceptor;import com.jfinal.aop.Interceptor;import com.jfinal.aop.Invocation;import javax.servlet.http.HttpServletRequest;public class RequestPool implements Interceptor { public static ThreadLocal<HttpServletRequest> localRequest= new ThreadLocal<>(); @Override public void intercept(Invocation inv) { localRequest.set(inv.getController().getRequest()); inv.invoke(); localRequest.remove(); } public static HttpServletRequest getRequest() { return localRequest.get(); }}
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 :
String para = RequestPool.localRequest.get().getParameter("username");
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 ~
边栏推荐
- 面试官:Redis中有序集合的内部实现方式是什么?
- C语言:#if、#def和#ifndef综合应用
- Reflection operation exercise
- OneNote 深度评测:使用资源、插件、模版
- 基于深度学习的参考帧生成
- 'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
- Why do job hopping take more than promotion?
- 【mysql】触发器
- Ravendb starts -- document metadata
- 快讯:飞书玩家大会线上举行;微信支付推出“教培服务工具箱”
猜你喜欢
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
[MySQL] trigger
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
【力扣刷题】一维动态规划记录(53零钱兑换、300最长递增子序列、53最大子数组和)
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
中国白酒的5场大战
Why does MySQL index fail? When do I use indexes?
20220211 failure - maximum amount of data supported by mongodb
Data Lake (VIII): Iceberg data storage format
Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
随机推荐
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
愛可可AI前沿推介(7.6)
跨分片方案 总结
Math symbols in lists
启动嵌入式间:资源有限的系统启动
OneNote in-depth evaluation: using resources, plug-ins, templates
How to implement common frameworks
Aike AI frontier promotion (7.6)
Replace Internet TV set-top box application through digital TV and broadband network
麦趣尔砸了小众奶招牌
[MySQL] trigger
C # use Oracle stored procedure to obtain result set instance
【力扣刷题】32. 最长有效括号
Why does MySQL index fail? When do I use indexes?
[MySQL] basic use of cursor
@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
每个程序员必须掌握的常用英语词汇(建议收藏)
技术分享 | 抓包分析 TCP 协议
Deployment of external server area and dual machine hot standby of firewall Foundation
Internet News: Geely officially acquired Meizu; Intensive insulin purchase was fully implemented in 31 provinces