Singleton Design Pattern VS Spring Singleton Scope

  • First off, the Singleton design pattern is.. well.. a design pattern whereas Spring’s singleton refers to the singleton scope which by the way is the default bean scope in Spring.
  • The Singleton design pattern is effectively a unique instance of an object per class loader whereas Spring singleton scope ensures the presence of a unique bean per application context.
  • Spring’s singleton scope is uniquely identified by the bean’s id meaning that we can have several beans of the same class type with different ids.
  • Spring’s singleton scoped bean is nothing more than a reference to an object, which, once instantiated, it gets cached by the IoC container and provided upon request. So in short the Spring container that deals with the beans lifecycle is handling internally the bean references and make a reference look like a singleton design pattern-ed object although it’s not.
  • We can have a situation where we inject a prototype-scoped bean into a singleton-scoped bean. That would mean, every time we are requesting the singleton-scoped bean a new object will be injected as a dependency for the prototyped-scoped dependency.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s