wood burning stoves 2.0*
The moose likes Spring and the fly likes regarding session scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "regarding session scope" Watch "regarding session scope" New topic
Author

regarding session scope

Amitosh Mishra
Ranch Hand

Joined: Feb 11, 2010
Posts: 49
Hi all,

i am working on spring mvc..

we are trying to inject a dependency(scope- session) in controller(scope-singleton) but it is not working properly.

we tried
also but it is injecting dependency based on the request for that dependency.

please provide help to resolve the issue.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Did you put aop:scoped proxy in the bean that is at the Session scope? That is where it goes so that the Singleton gets the proxy.

Also, for scope Session to work, it has to be in a web environment. If you are not in a Web Environment then an Exception will be thrown.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Why don't you post you xml config? Is you session bean in session scope? How did you determine that it is injecting in a request scope?
Are you confident that a new session isn't being created for the request? (if user has cookie's disabled and you are not using URL encoding?)


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Amitosh Mishra
Ranch Hand

Joined: Feb 11, 2010
Posts: 49
yes i am putting aop:scoped in bean configration.

I have mocked the web context to test my controller and there i am creating threads to test this.

i have few confusion also ..
1-As controllers are singleton so if i define any bean to be injected scoped session, will it really be instantiated for each session as there will be only one instance of controller will be created and all the dependencies will be injected at instantiation time only.
2- with aop scoped also the bean will be instantiated for every use not depending on scope defined in xml.

please help.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

will it really be instantiated for each session


Yes it will. That's what the <aop:scoped-proxy> is designed to do.

I would suggest that you read up this section in the documentation that explains how scoped proxies work.

Amitosh Mishra
Ranch Hand

Joined: Feb 11, 2010
Posts: 49
Thanks for the info Sam...
but is there any way i can control the bean injection depending on scope rather then default aop injection.
i thought of using method injection verifying the session from like below


but i want to know is there any way we can configure xml for this behavior.

thanks..
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

The correct way to define a session scope bean is.

See section called session scope in the spring documentation.
(Consider reading the entire chapter 4 of the spring documentation. That should help you understand Scoping rules better.)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

aop:scoped-proxy is the best cleanest way to get a bean injected into another bean at a different scope. It keeps your code POJO like and clean, you do not have to do anything different to the code whether it was session or request or singleton scope.

The old way before aop:scoped-proxy required you to tightly couple your code to Spring and write extra code. Not good for maintenance, not good for refactoring, just not a good way to do it. But I will tell you that way.

You can have your Controller implement ApplicationContextAware, it has one method setApplicationContext(ApplicationContext context). You assign the context to an instance variable of your controller, and anytime you need that bean that is scoped at session you call context.getBean()

Also, note that you are running tests, which are not in a Web environment. So you mock it out and pretend so that you don't get an exception. So you will have very short lived HttpSessions, so much that they will look exactly like Requests, in my opinion.

Mark
Amitosh Mishra
Ranch Hand

Joined: Feb 11, 2010
Posts: 49
i verified the same in application and session beans are injected properly..

I think the issue is with mocked context..

Thanks Mark/Sam for your quick reply..

regards
Amitosh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding session scope
 
Similar Threads
Struts session
Maven web project dependencies
HTTP authentication on accessing WSDL
Maven dependencies
Unable to create one single jar file in maven with dependencies included inside