Pages

July 23, 2018

Spring Singleton vs Singleton Pattern

What is the difference between Spring Singleton and Java Singleton?
Difference between Spring Singleton and Java Singleton is one of the common misunderstanding among Spring Framework developers.

If you are not already familiar about the Singleton design pattern, please read the blog-post related to various Design Patterns.



Singleton scope is the default scope in Spring and it is not same as singleton pattern.

The Singleton pattern ensures one instance of a particular class of per class loader. Where as the Spring Singleton is per container per bean, i.e Spring guarantees to create only one bean instance for given bean id definition per container.

Singleton instance in Spring will be stored in a cache, and all subsequent requests and references for that named bean will result in the cached object being returned.

In short:
  • Singleton pattern is scoped by per Java class per class loader.
  • Singleton bean scope is scoped by per Spring container.
-K Himaanshu Shuklaa..

No comments:

Post a Comment