@Scheduled实现定时任务时报TaskUtils$LoggingErrorHandler错
2018-01-18 17:02:00,026 [pool-1-thread-1] ERROR: org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
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)
...
我的aop日志记录方法中注入了Request
,导致此报错!
Scheduled
是异步线程来处理你的逻辑的,而我们代码中使用了Request
,currentRequestAttributes
方法是需要有request
和response
绑定到当前的线程中,也就是说只有http请求的时候才不会报错。但我们是异步的,肯定没有这个。所以会报错。
更改架构,避免使用到Request