java.lang.IllegalStateException:找不到线程绑定的请求,方面是异常 [英] java.lang.IllegalStateException: No thread-bound request found, exception in aspect
问题描述
以下是我的方面:
@Configurable
@Aspect
public class TimingAspect {
@Autowired
private HttpServletRequest httpServletRequest;
// Generic performance logger for any mothod
private Object logPerfomanceInfo(ProceedingJoinPoint joinPoint, String remoteAddress) {
StringBuilder tag = new StringBuilder();
if (joinPoint.getTarget() != null) {
tag.append(joinPoint.getTarget().getClass().getName());
tag.append(".");
}
tag.append(joinPoint.getSignature().getName());
StopWatch stopWatch = new StopWatch(tag.toString());
Object result = joinPoint.proceed(); // continue on the intercepted method
stopWatch.stop();
PerformanceUtils.logInPerf4jFormat(stopWatch.getStartTime(), stopWatch.getElapsedTime(), stopWatch.getTag(), stopWatch.getMessage(), remoteAddress);
return result;
}
@Around("execution(* $$$.$$$.$$$.api.controller.*.*(..))")
public Object logAroundApis(ProceedingJoinPoint joinPoint) throws Throwable {
String remoteAddress = null;
if (httpServletRequest != null) {
remoteAddress = httpServletRequest.getRemoteAddr();
}
return logPerfomanceInfo(joinPoint, remoteAddress);
}
@Around("execution(* $$$.$$$.$$$.$$$.$$$.$$$.*(..))")
public Object logAroundService(ProceedingJoinPoint joinPoint) throws Throwable {
String remoteAddress = null;
if (httpServletRequest != null) {
remoteAddress = httpServletRequest.getRemoteAddr();
}
return logPerfomanceInfo(joinPoint, remoteAddress);
}
我没有收到任何编译时错误,但是在启动码头服务器时出现以下异常:
I do not get any compile time errors but I do following exception when I start my jetty server:
嵌套的异常是java.lang.IllegalStateException:没有线程绑定 找到请求:您是指请求属性之外的 实际的Web请求,或处理原始请求之外的请求 接收线程?如果您实际上是在网络请求中进行操作 仍然收到此消息,则您的代码可能正在外部运行 的DispatcherServlet/DispatcherPortlet:在这种情况下,请使用 RequestContextListener或RequestContextFilter公开当前 请求.
nested exception is 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.
这里要注意的一件事是,如果删除"logAroundService"方法,则不会获得任何异常.
One thing to note here is, if I remove "logAroundService" method, I do not get any exceptions.
推荐答案
您不应在方面自动连接HttpServletRequest
,因为这会将您的方面与只能在执行中的
You shouldn't autowire a HttpServletRequest
in your aspect as this will tie your aspect to be only runnable for classes that are called from within an executing HttpServletRequest
.
在需要时使用RequestContextHolder
获取请求.
Instead use the RequestContextHolder
to get the request when you need one.
private String getRemoteAddress() {
RequestAttributes attribs = RequestContextHolder.getRequestAttributes();
if (attribs instanceof NativeWebRequest) {
HttpServletRequest request = (HttpServletRequest) ((NativeWebRequest) attribs).getNativeRequest();
return request.getRemoteAddr();
}
return null;
}
这篇关于java.lang.IllegalStateException:找不到线程绑定的请求,方面是异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!