How to solve the problem that custom thread RequestContextHolder.getRequestAttributes returns null?

problem description

use the Spring framework, start a new thread in Service, and use
RequestAttributes ra = RequestContextHolder.getRequestAttributes ();
in the new thread to get null,. Is there any way to solve it?

the environmental background of the problems and what methods you have tried

In

Service, multithreading is used to optimize the response time, and the RequestContextHolder class is used in some threads, and the return value is empty.

related codes

RequestAttributes ra = RequestContextHolder.getRequestAttributes ();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest ();
System.out.println (request.getContextPath ());

what result do you expect? What is the error message actually seen?

expectation: System.out.println (request.getContextPath ()); prints out the path normally,
actual: RequestAttributes ra = RequestContextHolder.getRequestAttributes ();

  ranull.
Aug.27,2021

before starting a new thread, add code:
/ / set the RequestAttributes object to child thread sharing
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes ();
RequestContextHolder.setRequestAttributes (sra, true);

Menu