• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

SPRING - Sharing singleton with prototype - Possible?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,

I have a requirement where two business classes should share the same value object. And these two business classes should be instantiated one per request and value object is common for both these.

<bean id="bus1" class="com.foo.Business1" scope="prototype">
<property name="account" ref="acct"/>
</bean>
<bean id="bus2" class="com.foo.Business2" scope="prototype">
<property name="account" ref="acct"/>
</bean>
<bean name="acct" class="com.foo.Account" scope="singleton">
</bean>

I need this acct to be singleton for bus1 and bus2 instances and not for entire IOC container. In this way, when a user logs on, bus1 and bus2 will share the same acct. And for another user, a different acct instance has to be created, but shared between bus1 and bus2.

Is this possible in Spring or should i do it programmatically?

Kindly help.

Regards,
venkat
 
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
Yes, it is possible. In the case of a Prototype getting injected a Singleton, you don't have to do anything. As far as a Prototype being injected into the Singleton. Then you just implement the ApplicationContextAware interface. Then your Singleton will get a reference to the ApplicationContext, and you can just call getBean() on it in your Singleton to get a new Prototype instance.

Mark
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the term "Singleton" is used very loosely here. The fact that you need a new Account object for every user makes it anything but "Singleton". Look into the "Session" or "Request" scope. Session scope will maintain one Account object for the entire HTTP Session and Request scope will create a new Account object for every request.
 
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
I agree. Session scope sounds like what the user really wants. SIngleton is if you want just one instance for the container for all users.

Mark
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic