aspose file tools*
The moose likes Spring and the fly likes How does Spring framework handle Singleton in its implementation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How does Spring framework handle Singleton in its implementation?" Watch "How does Spring framework handle Singleton in its implementation?" New topic
Author

How does Spring framework handle Singleton in its implementation?

Silvio Esser
Ranch Hand

Joined: Nov 05, 2005
Posts: 58
Using the Singleton/Double Checking Locked pattern?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Silvio Esser
Ranch Hand

Joined: Nov 05, 2005
Posts: 58
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
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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
 
wood burning stoves
 
subject: How does Spring framework handle Singleton in its implementation?