jQuery in Action, 3rd edition
The moose likes Spring and the fly likes question on prototype scope of a bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "question on prototype scope of a bean" Watch "question on prototype scope of a bean" New topic

question on prototype scope of a bean

Mark Curlette
Ranch Hand

Joined: May 26, 2011
Posts: 46


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

Joined: Mar 07, 2010
Posts: 1682

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.

[How To Ask Questions][Read before you PM me]
I agree. Here's the link: http://aspose.com/file-tools
subject: question on prototype scope of a bean
It's not a secret anymore!