• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Not able to instantiate/inject session scoped spring bean into Spring controller.

 
Ranch Hand
Posts: 36
Hibernate Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is regarding migration of a simple websphere flowers application to Spring MVC.

The shopping cart which is spring bean is session scoped. and is being injected into the Shopping cart controller.

The context file looks like below








When deployed, i get error
Error Stack Trace:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopping': Scope 'session' is not ac
tive for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested excep
tion is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual we
b request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and s
till receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestConte
xtListener or RequestContextFilter to expose the current request.

so googled a bit, sought suggestion from peers....
it seems there's clash of session scoped bean being injected in Singleton scoped controller.

so the solution is include the following i.e <aop:scoped-proxy/> in the context xml file.



but how do you put the same using annotation?

Help sought and appreciated!

Thanks
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to use annotations you would have to define your beans using java config.

This would be equivalent to what you did in the XML but using java config instead

 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bill Gorder wrote:If you want to use annotations you would have to define your beans using java config.

This would be equivalent to what you did in the XML but using java config instead



Um. Wouldn't @Component instead of @Bean also work?

@Component and @Scope can be on a bean class.

Mark
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

You are of course correct, I reread my response and said 'have to' instead of 'could' I should know better than to type in absolutes With Spring there is usually several ways to solve the same problem. I guess i just got over excited for an opportunity to promote java config


 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!