Spring : Singleton VS Prototype

4.5k views Asked by At

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 !

4

There are 4 answers

0
Prathap On BEST ANSWER

Same instance would mean any call (from anywhere) to getBean() from ApplicationContext or BeanFactory 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.

0
Kiran Kumar On

It helps in designing a software, refer java design patterns you fill find much useful information, In case of singleton you will be able to create only object, it will helps cases like 1.saving time in creating a very complex object (we can reuse the same object) 2.Some times we need to use the same object throughout the application E.g if you want count the total number of active users in the application, you can use singleton object to store it, because there will be only one object so you can easily update and retrieve data. In case of Prototype it will always give you different object, it is necessary in some cases like some access token, you should always get the new /valid token to proceed further, so prototype pattern /Bean is useful.

2
bsiamionau On

In case if the same instance is injected everywhere as Singleton, you can use it's shared state for containing any kind of data. In case if new instance is created any time bean is being injected - it's state is not shared. By default all beans are Singletons. Prototype scope stands for cases when bean's inner data should be unique for each place you inject bean into.

Example: you have the bean which represents REST client. Different parts of application use different REST services, each requires some specific request headers - for security purposes, for example. You can inject the same REST client in all these beans to have it's own REST client bean with some specific headers. At the same time you can configure client's politics in common for the whole application - request timeouts, etc.

See also: When to use Spring prototype scope?

0
Will Hu On

My understanding for Singleton is more likely used in the situation like return single Utility instance.

//SomeUtil is Singleton
var util = SomeUtil.getInstance();
print(util.doTask1());
print(util.doTask2());

If another thread goes into these code, SomeUtil.getInstance() just return the SomeUtil instance other than creating a new one (which might cost much to create a new one).

As to Prototype, I just found this situation using Prototype:

Lets understand this pattern using an example. I am creating an entertainment application that will require instances of Movie, Album and Show classes very frequently. I do not want to create their instances everytime as it is costly. So, I will create their prototype instances, and everytime when i will need a new instance, I will just clone the prototype.

Example code locates https://github.com/keenkit/DesignPattern/tree/master/src/PrototypePattern