• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

question on prototype scope of a bean

Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
Posts: 1682
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
    Bookmark Topic Watch Topic
  • New Topic