Difference between Singleton and Prototype in Java Spring

 

SingletonPrototype
Only one instance is created for a single bean definition per Spring IoC containerA new instance is created for a single bean definition every time a request is made for that bean.
Same object is shared for each request made for that bean. i.e. The same object is returned each time it is injected.For each new request a new instance is created. i.e. A new object is created each time it is injected.
By default scope of a bean is singleton. So we don’t need to declare a been as singleton explicitly.By default scope is not prototype so you have to declare the scope of a been as prototype explicitly.
Singleton scope should be used for stateless beans.While prototype scope is used for all beans that are stateful

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence