Spring request scope error : No thread-bound request found
Problem
Developing web application with Spring, make a bean with scope of “request“.
@Component
@Scope("request")
public class PaymentService {
@Autowired
UserBo userBo;
//...
But hit following error message?
Caused by: 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.
//... 28 more
Solution
To fix it, register a RequestContextListener listener in web.xml
file.
File : web.xml
<web-app ...>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
</web-app>
Maybe you could provide an explanation about which is the cause of the problem and why that solves it. It would be great to know if is the same context or not. At least for me it didn’t worked. Even in that way thanks for sharing.
what to do if I am usinf spring boot application… As it don’t have web.xml.. Please help as soon as possible
Create Java class which will extend WebAppInitializer and override onStartUp method and add RequestContextListner to servletContext object.
public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addListener(new RequestContextListener());
}
}
Thanks.
problem: spring-aop isn’t on the classpath. Error message ClassNotFoundException would have made it much more clear.
Add “http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd” to schema location.
Also, bean should have configuration:
In the grand scheme of things you receive an A with regard to effort. For right now I will yield to your issue.
The solution mentioned didn’t worked. Can you suggest something else ?
Hello Sir,
I`m getting this error in a JUnit test, and i`m using spring 2.5, so I can`t add an entire WebApplicationContext from web.xml. I was successfully added all XML bean config files, but NOT web.xml.
If you know how to add RequestContextListener programmatically instead of changing web.xml, please, let me know. I`m working on it, but I can`t get it for now.
P.S. I know, that JUnit is not supposed to use for functional tests and to test entire application, but that`s my current task, and I can`t change it.
Thanks in advance,
Lexx
OK, i`ve found a partial solution.
(to initialize request scope before calling a servlet service() method and to destroy it later)
But if you know more programmer-friendly solution, please let me know.
Hello Sir,
Me too getting same error, It still not resoved.
i have already added listener in web.xml… please tell me where i am wrong.
Thanks in advance.
Hello Sir,
I have added listner in web.xml still i am getting same error on WAS server. its working fine for tomcat. please help me how can i resolve this error ??
Regards
Puja