Difference between Singleton and Prototype in Java Spring
Singleton | Prototype |
---|---|
Only one instance is created for a single bean definition per Spring IoC container | A 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
Post a Comment