File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes SPRING - Sharing singleton with prototype - Possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "SPRING - Sharing singleton with prototype - Possible?" Watch "SPRING - Sharing singleton with prototype - Possible?" New topic
Author

SPRING - Sharing singleton with prototype - Possible?

Venkataramanan Amirthalingam
Greenhorn

Joined: Nov 27, 2008
Posts: 20
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
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Senthil S Kumar
Greenhorn

Joined: Aug 13, 2006
Posts: 10
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
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SPRING - Sharing singleton with prototype - Possible?
 
Similar Threads
running before I can crawl ( or how to add Spring to an existing Hibernate standalone app)
Spring + struts2 DI problem
Spring + JPA + Hibernate and multiple Database connection
handling ContextSource & DataSource Transaction using @Transactional annotation.
[Security:090398]Invalid Subject: weblogic :java.lang.SecurityException: [nested exception is java.l