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>

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

12 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Gabriel Q.
11 years ago

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.

pragati
7 years ago

what to do if I am usinf spring boot application… As it don’t have web.xml.. Please help as soon as possible

Krishan Gandhi
7 years ago
Reply to  pragati

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

Mohit
11 years ago

Thanks.

Sunil Netra
12 years ago

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.

Sunil Netra
12 years ago
Reply to  Sunil Netra

Also, bean should have configuration:

tutu skirts
13 years ago

In the grand scheme of things you receive an A with regard to effort. For right now I will yield to your issue.

Koustav
13 years ago

The solution mentioned didn’t worked. Can you suggest something else ?

Lexx
13 years ago

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

Lexx
13 years ago
Reply to  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.

Kishor
13 years ago

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.

puja
13 years ago

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