当前位置:网站首页>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 ~

原网站

版权声明
本文为[Huawei cloud developer Alliance]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061258323731.html