Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams 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:

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.

One thing to note here is, if I remove "logAroundService" method, I do not get any exceptions.

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;
                Remember to add the following listener in the web.xml (otherwise attribs will be null): <listener> 	<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
– fl4l
                Dec 21, 2015 at 16:08

@M. Deinum answer doesn't work for me. I use these codes instead

RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
if (attributes != null) {
    HttpServletRequest request = ((ServletRequestAttributes) attributes).getRequest();
    return request.getRemoteAddr();

Create bean for RequestContextListener. I got the same error for autowiring HttpServletRequest And the following two lines of code works for me

@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();

As the error message said: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

To fix it, register a RequestContextListener listener in web.xml file.

<web-app ...>
   <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
   </listener>
</web-app>
                This worked for me in weblogic 10.3.6 with a relatively recent version of spring boot (1.4)
– Andrea Bergia
                Nov 30, 2017 at 9:40

With your pointcut expression, you're basically proxying every bean and applying that advice. Some beans exist and operate outside the context of an HttpServletRequest. This means it cannot be retrieved.

You can only inject the HttpServletRequest in places where a Servlet container request handling thread will pass through.

all the methods intercepted by all pointcuts are called in the same servlet container request handling thread. – hrishikeshp19 Jun 3, 2014 at 22:24 @riship89 Post your full stack trace to prove it. The other possibility is that you have your aspect loaded by the ContextLoaderListener ApplicationContext rather than the DispatcherServlet's. – Sotirios Delimanolis Jun 3, 2014 at 22:26 @riship89 With @Autowired you are setting an injection target. You are saying I want this to be managed by Spring. Well, in this case, Spring can't manage it so it crashes. – Sotirios Delimanolis Jun 3, 2014 at 22:27 Okay, I get it now. many of my beans are in application context and not in servlet context. Now, how can I expose httpservlet request in my whole application? – hrishikeshp19 Jun 3, 2014 at 23:53

I solved by adding the following:

@Around(value = "@annotation(JwtSecure)")
@Pointcut("within(@org.springframework.stereotype.Repository *)"
        + " || within(@org.springframework.stereotype.Service *)"
        + "|| within(@org.springframework.stereotype.Component *)"
        + " || within(@org.springframework.web.bind.annotation.RestController *)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.