Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton vs prototype

 
Naveen Madarapu
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i inject prototype bean into a singleton bean if insert what happen can any one explain by using small code........
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while defining our beans can i add prototype bean reference into singleton bean?
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.
 
Naveen Madarapu
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks bill i got that point ........................
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic