i'm new to Spring starting to learn new concepts , and i found topic speaking about bean Scopes : - Singleton : returns the same instance every time. - Prototype : returns new instance of the object per every request.
my question is : how is this helpful for me what is the difference between the same instance , and new instance of the object , or why the prototype scope exists !
Same instance would mean any call (from anywhere) to
getBean()
fromApplicationContext
orBeanFactory
will land you with the same instance, i.e. constructor is called just once when Spring is being initialized (per Spring container).However, there are scenarios when you would want different object instances to work with.
For example, if you have a Point object as a member variable in a Triangle class, in case of Singleton, when the Triangle class is being instantiated, the Point object also is instantiated as it is dependent.
If you require a different instance of Point to work with elsewhere, then you will need to define the Point as a prototype, else it carries the same state.
Googling would surely help you find answers and examples demonstrating the use case.
Hope this helps.