What is the difference between a Spring singleton and a Java singeleton(design pattern)? [duplicate]
3
|
This question already has an answer here:
I am learning Spring framework and currently reading a book about it. In this book it says that a Spring singleton is different from a Java singleton? What does this mean and what are the differences? Thanks
| ||||
|
marked as duplicate by C. Ross, Jayan, ataylor, Godeke, Andrew AylettMar 6 '13 at 17:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4
|
The Java singleton is scoped by the Java class loader, the Spring singleton is scoped by the container context.
Which basically means that, in Java, you can be sure a singleton is a truly a singleton only within the context of the class loader which loaded it. Other class loaders should be capable of creating another instance of it (provided the class loaders are not in the same class loader hierarchy), despite of all your efforts in code to try to prevent it.
In Spring, if you could load your singleton class in two different contexts and then again we can break the singleton concept.
So, in summary, Java considers something a singleton if it cannot create more than one instance of that class within a given class loader, whereas Spring would consider something a singleton if it cannot create more than one instance of a class within a given container/context.
| ||||||||||||||||||||||
add comment
|