0

Singleton Pattern maintains the instance at Spring Container Level, while the Singleton Design Pattern maintains it at Class Loader Level.

Is any other difference?

Next, I still think there the above reason in not a valid reason. It is just the matter of fact that one Application Context/Container is loaded in one Class Loader only. Thus technically there is no difference. Is that correct or I am missing something?

Ref: Singleton design pattern vs Singleton beans in Spring container

Community
  • 1
  • 1
Sandeep Jindal
  • 14,510
  • 18
  • 83
  • 121
  • Singleton pattern is per class loader level, while Singleton bean scope is per spring container. ---- http://www.javabench.in/2012/04/difference-between-singleton-design.html – Raúl May 18 '16 at 11:16

5 Answers5

2

Well, the real difference is not around class loading, but it's about the design principle. Singleton pattern has it's own limitations. It exposes an object globally as well as it's difficult to test. But, singleton through DI framework like Spring or Guice is free from those problems.

This SO thread may help you to understand. As well as Google-singleton-detector and Misko Hevery's blog are also interesting read.

Community
  • 1
  • 1
Arnab Biswas
  • 4,495
  • 3
  • 42
  • 60
1

Using a "real" singleton is more limiting in the sense that you are then bound to be able to create only a single instance of that class (within a classloader).

If you use Spring singleton-scoped beans, you can create as many "singleton" instances of that class you like (as long as the bean class is not a real singleton).

Hence they are not technically the same thing.

Jukka
  • 4,583
  • 18
  • 14
  • Not sure I get that. "You can create as many "singleton" instances of that class"? I can create many instances of singleton? – Sandeep Jindal Jun 13 '13 at 14:48
0

This is mostly the name that these two have in common. Singleton pattern ensures a class has only one instance, while Spring's singleton bean scope just instructs container to use the single instance of a bean during dependency injection, the bean can be any class with no restrictions.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Singeton pattern is a specially designed class with private constructors, public static global access point etc, Spring singleton is a POJO, any class – Evgeniy Dorofeev Jun 13 '13 at 14:57
0

A spring singleton ensures that only one instance of the object is created as long as the object is created using spring framework. On the contrary implementing the singleton pattern ensures that only one instance of the object is present.

I have seen code where objects were defines as singleton in spring configuration. The singleton object was some times created using spring DI and other times using new operator.

Hence caution is required to ensure that such kinds of abuses are not made and single instance of singletons be maintained.

sri
  • 27
  • 6
0

Singleton pattern is described at per class loader level. Singleton bean scope is per spring container.

http://www.javabench.in/2012/04/difference-between-singleton-design.html

Raúl
  • 1,542
  • 4
  • 24
  • 37