java.lang.IllegalStateException: No thread-bound request found, exception in aspect

前端 未结 5 1164
不知归路
不知归路 2021-02-02 08:20

Following is my aspect:

    @Configurable
    @Aspect
    public class TimingAspect {

        @Autowired
        private HttpServletRequest httpServletRequest;
         


        
5条回答
  •  借酒劲吻你
    2021-02-02 09:06

    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.

    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;
    }
    

提交回复
热议问题