java.lang.IllegalStateException:未找到线程绑定请求,方面有异常

ris*_*p89 23 java spring aspectj spring-mvc

以下是我的方面:

    @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);
        }
Run Code Online (Sandbox Code Playgroud)

我没有得到任何编译时错误,但是当我启动我的jetty服务器时,我做了以下异常:

嵌套异常是java.lang.IllegalStateException:找不到线程绑定请求:您是指在实际Web请求之外的请求属性,还是在最初接收线程之外处理请求?如果您实际在Web请求中操作并仍然收到此消息,则您的代码可能在DispatcherServlet/DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter来公开当前请求.

这里要注意的一件事是,如果我删除"logAroundService"方法,我不会得到任何例外.

M. *_*num 16

您不应该HttpServletRequest在自己的方面自动装配,因为这会将您的方面绑定为仅对在执行中调用的类可运行HttpServletRequest.

而是RequestContextHolder在需要时使用它来获取请求.

private String getRemoteAddress() {
    RequestAttributes attribs = RequestContextHolder.getRequestAttributes();
    if (attribs instanceof NativeWebRequest) {
        HttpServletRequest request = (HttpServletRequest) ((NativeWebRequest) attribs).getNativeRequest();
        return request.getRemoteAddr();
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)


Avi*_*was 9

为 RequestContextListener 创建 bean。我在自动装配 HttpServletRequest 时遇到了同样的错误,以下两行代码对我有用

@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
Run Code Online (Sandbox Code Playgroud)


use*_*407 8

@M.Deinum的回答对我不起作用.我改用这些代码

RequestAttributes attribs = RequestContextHolder.getRequestAttributes();
if (RequestContextHolder.getRequestAttributes() != null) {
    HttpServletRequest request = ((ServletRequestAttributes) attributes).getRequest();
    return request.getRemoteAddr();
}
Run Code Online (Sandbox Code Playgroud)


Nic*_* Lu 7

如错误消息所述:在这种情况下,请使用RequestContextListener或RequestContextFilter公开当前请求。

要解决此问题,请在web.xml文件中注册一个RequestContextListener侦听器。

<web-app ...>
   <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
   </listener>
</web-app>
Run Code Online (Sandbox Code Playgroud)