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);

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b44713-34ac3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b44713-34ac3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?