版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
在 Spring中使用 多线程中, 应注意 新起的线程是不能直接访问主线程的生命周期结束的变量
比如在 在 web应用中 在新起的线程中使用 HttpServletRequest , 会报出 IllegalStateException 的异常, 需要在使用 Request 的地方捕获异常, 或者把Request的对象传递到新起的线程里面
java.lang.IllegalStateException: No thread-bound request found: Are you referring to
request attributes outside of an actual web request, or processing a request outside
of the originally receiving thread? If you are actually operating within a web request
and still receive this message, your code is probably running outside of
DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or
RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:309)
at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:64)
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:325)
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:320)
at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:307)
at com.sun.proxy.$Proxy172.getHeader(Unknown Source)