aspose file tools*
The moose likes Spring and the fly likes Singleton vs prototype Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Singleton vs prototype" Watch "Singleton vs prototype" New topic
Author

Singleton vs prototype

Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
Posts: 64

can i inject prototype bean into a singleton bean if insert what happen can any one explain by using small code........


OCPJP
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Naveen Madarapu wrote:can i inject prototype bean into a singleton bean if insert what happen can any one explain by using small code........


Would you mind restating your question? It is hard to get what you are asking. Thank you.
Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
Posts: 64

while defining our beans can i add prototype bean reference into singleton bean?
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Naveen Madarapu wrote:while defining our beans can i add prototype bean reference into singleton bean?


Why not?

You can. But because it is a singleton, everytime you get the prototype inside the singleton, it will return the same instance of the prototype.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1676
    
    7

What Jesus said is true, but what if you need need the singleton bean to acquire a new instance of the prototype bean repeatedly at runtime? You can do this as well using method injection. The following (from spring docs) shows a singleton bean 'CommandManager' which through method injection grabs a new instance of the prototype bean 'AsyncCommand' every time at runtime. Note I showed you the java config way because I prefer it, but you can find the XML way as well in the spring documentation. The trick to the way this works is the way that createCommand() is overridden.

CommandManger (Singleton)


Excerpt from the configuration class:



[How To Ask Questions][Read before you PM me]
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi,

There is also another way to achieve this by using AOP proxy. It's just a configuration on xml file, but you need to have CGlib jar on your project classpath.

take a look at the section 4.4.4.5 Scoped beans as dependencies of the Reference doc


Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1676
    
    7

Hemant brings up a good point and I has thought of bringing it up as well but I did not mention it since the asker specifically targeted singleton and prototype beans. AOP proxy is more geared towards session or custom scoped beans.

From the docs you referenced.



But that is not to say that you can't use it with a prototype bean. Rather than putting AsyncCommand as a bean as I did in the first example you would slap an @Component on the class and add @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
*Note to proxy your class in this fashion you need CGLIB on your classpath but I always have that anyway since I use java config.

The XML way is clearly shown in the doc link Hemant gave, I showed the annotation based approach just as an alternative.
Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
Posts: 64

Hello thanks for replays.. but actually my question is

consider example
bean A is singleton scope
bean B is prototype scope
if i inject B into A . how can i say A is Singleton.

singleton means only one instance created by the container and prototype means it varies from request to request wright ?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1676
    
    7

Only one instance of A is ever created.

This is a nasty example but I think it illustrates the point consider the following.



The class Singleton is a singleton the new operator is only called once ever. After that the same instance is used by calling the getInstanceMethod. Despite that fact SomeObject is new every time the instance is retrieved.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
.
Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
Posts: 64

thanks bill i got that point ........................
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton vs prototype