Feb 8th, 2009, 01:10 PM
Request / Session Scope bean initialization
I am interested in using the request and/or session scoped beans functionality for a project I'm working on and saw the documentation specifically for user preferences in the spring doc.
My question is how would I create and initialize this bean before putting it in the session (or request scope) so that it can be used by other components in the spring mvc project ?
So lets say when every request is received there are attributes of the authenticated user making the request set in request headers. What I would like to do is before the request hits any of my spring controllers, to be able to pull out the values of all of the relevant headers and create a User object and place it in the request scope. This way when all of my controllers and any class called downstream needs to access details of the user, all they have to do is pull out the User request scope bean and access its values. How would I go about doing this ? I know how to dependency inject this bean into my controllers but do I fire the initialization logic that reads the headers and creates the bean instance and how do I make sure this gets set in the request scope before the controllers are called ?
I feel the dependency injection of session and request scope beans is very powerful but where would I put in logic to have these beans created and placed in the request / session ?
Take the case of user preferences described in the spring doc. I see the value of creating a session scoped bean and injecting this into all the classes that need it. But how do I fire off the logic that creates this UserPReference bean in the first place ? i.e. a users preferences is typically a db operation where I would query the db for the various preferences set for this user. How would I make sure this logic fires on session creation to create the UserPreference bean before putting it in the session for access by other classes ?
Any insights would be much appreciated.
Oct 22nd, 2009, 01:52 AM
Initialization of request/session beans
Originally Posted by Craze04
I feel the same way: the injection of session/request beans via proxy is very powerful but I've your same problem : I'm unable to initialize it in the way I need.
I tested with filter and dispatcher servlet interceptors to do it but the process failed with this error:
Autowiring of methods failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'scopedTarget.eaRequest': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; 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.
I still have to test the suggested solution because I don't like it very much: I find that the right way to do it should be in an interceptor...
Anyway, have you solved your problem in a way or another?