問(wèn)題描述
使用Spring框架,在Service中開啟一個(gè)新的線程,在新的線程中使用RequestAttributes ra = RequestContextHolder.getRequestAttributes();獲取出來(lái)為null,有沒(méi)有什么辦法能解決?
問(wèn)題出現(xiàn)的環(huán)境背景及自己嘗試過(guò)哪些方法
Service中為優(yōu)化響應(yīng)時(shí)間,使用了多線程,部分線程中使用到了RequestContextHolder類,結(jié)果返回值為空。
相關(guān)代碼
RequestAttributes ra = RequestContextHolder.getRequestAttributes();ServletRequestAttributes sra = (ServletRequestAttributes) ra;HttpServletRequest request = sra.getRequest();System.out.println(request.getContextPath());
你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?
期待:System.out.println(request.getContextPath());正常打印出路徑,實(shí)際:RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ra為null.
怎么解決自定義線程 RequestContextHolder.getRequestAttributes返回null?
鴻蒙傳說(shuō)
2019-01-17 01:33:46