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

question on prototype scope of a bean

 
Mark Curlette
Ranch Hand
Posts: 46
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am new to Spring and I have question regarding to the prototype scope of a bean.
I have a simple program, and i have a simple bean called helloworld which has a prototype scope.
Even though its scope defined as prototype; it did not created a new instance each times the bean is called.
I thought it should create a new instance every time it is called.
I was running this program in eclipse.

Here is my simple code. Please help me to point out my errors if you could.

Many thanks in advance.







and the result when i ran the program:



I don't know why its showed the same message twice as it was singleton. I thought it should shown Hello world on the first line and null on the second line.
 
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
Its going to print it twice you have it defined in your bean


What proto-type does is it does not use a singleton. That bean is created new each time you ask for it. To prove this add a default constructor to your HelloWorld object


You will see that the constructor is called twice. Once each time you get the bean. Now if you remove the prototype it will only print once. This is because it is instantiated only once as a singleton and you get that same instance on each request.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic