• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How does Spring framework handle Singleton in its implementation?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the Singleton/Double Checking Locked pattern?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually very simply. Spring is creating your instance, and no one can access the beans until the full ApplicationContext or BeanFactory is completely created. So you won't ever have more than one thread trying to "get" the instance at the same time that it is being created.

Mark
 
Silvio Esser
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if I have two requests that access the singleton bean at the same time? Does one request have to wait
until the other to finish. How does Spring container find the singleton bean instance for my requests? Using static method or
variable?
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Silvio Esser wrote:What if I have two requests that access the singleton bean at the same time? Does one request have to wait
until the other to finish. How does Spring container find the singleton bean instance for my requests? Using static method or
variable?



Multiple threads can run at the same time calling methods of that class. The key here is the bean classes that you define in Spring xml will be your Services, Repository/DAO which should never hold state. For instance, a domain object would not be defined in your xml as a Spring bean. So there are no threading issues or need for synchronization.

Now if you have a bad design and actually hold state in those classes, then you will have issues, but you would have those issues with or without Spring. If you do hold state, then you might want a different scope than Singleton.

Mark
reply
    Bookmark Topic Watch Topic
  • New Topic