• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regarding session scope

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?)
 
Amitosh Mishra
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The City calls upon her steadfast protectors. Now for a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic