• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Spring Singleton or Java Singleton

 
Ranch Hand
Posts: 401
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference in Spring Singleton and Java Singleton? I understand that Spring Singleton is 1 instance per spring container per JVM while Java Singleton is 1 instance per JVM.

I am just wondering in Spring MVC application, how can we have more than 1 spring containers?
 
Bartender
Posts: 1150
38
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be more precise, a java singleton is an unique instance per classloader, and a JVM may have different classloaders.
Beside this, I can't think of other important differences. Maybe, to develop a Spring singleton is easier than developing a 'regular' java singleton, just because it's only matter to define a scope = singleton on a bean and let the container do the work for you.
 
author & internet detective
Posts: 39343
755
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:What is the difference in Spring Singleton and Java Singleton?


Not all Spring singletons are Java singleton. A Java singleton is coded in a way that prevents code from creating two instances of the object.

A Spring singleton just means that Spring will only create one object. There's nothing to stop other Java code from creating other instances. Think of it as object pooling/caching.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!