Package com.google.inject.internal
Class SingletonScope
- java.lang.Object
-
- com.google.inject.internal.SingletonScope
-
- All Implemented Interfaces:
Scope
public class SingletonScope extends Object implements Scope
One instance perInjector
. Also see@
Singleton
.Introduction from the author: Implementation of this class seems unreasonably complicated at the first sight. I fully agree with you, that the beast below is very complex and it's hard to reason on how does it work or not. Still I want to assure you that hundreds(?) of hours were thrown into making this code simple, while still maintaining Singleton contract.
Anyway, why is it so complex? Singleton scope does not seem to be that unique.
- Guice has never truly expected to be used in multi threading environment with many Injectors working alongside each other. There is almost no code with Guice that propagates state between threads. And Singleton scope is The exception.
- Guice supports circular dependencies and thus manages proxy objects. There is no interface that allows user defined Scopes to create proxies, it is expected to be done by Guice. Singleton scope needs to be able to detect circular dependencies spanning several threads, therefore Singleton scope needs to be able to create these proxies.
- To make things worse, Guice has a very tricky definition for a binding resolution when Injectors are in in a parent/child relationship. And Scope does not have access to this information by design, the only real action that Scope can do is to call or not to call a creator.
- There is no readily available code in Guice that can detect a potential deadlock, and no code for handling dependency cycles spanning several threads. This is significantly harder as all the dependencies in a thread at runtime can be represented with a list, where in a multi threaded environment we have more complex dependency trees.
- Guice has a pretty strong contract regarding Garbage Collection, which often prevents us from linking objects directly. So simple domain specific code can not be written and intermediary id objects need to be managed.
- Guice is relatively fast and we should not make things worse. We're trying our best to optimize synchronization for speed and memory. Happy path should be almost as fast as in a single threaded solution and should not take much more memory.
- Error message generation in Guice was not meant to be used like this and to work around its APIs we need a lot of code. Additional complexity comes from inherent data races as message is only generated when failure occurs on proxy object generation. Things get ugly pretty fast.
- Author:
- timofeyb (Timothy Basanov)
- See Also:
scope(Key, Provider)
,CycleDetectingLock
-
-
Constructor Summary
Constructors Constructor Description SingletonScope()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Provider<T>
scope(Key<T> key, Provider<T> creator)
Provides singleton scope with the following properties: creates no more than one instance per Key as a creator is used no more than once result is cached and returned quickly on subsequent calls exception in a creator is not treated as instance creation and is not cached creates singletons in parallel whenever possible waits for dependent singletons to be created even across threads and when dependencies are shared as long as no circular dependencies are detected returns circular proxy only when circular dependencies are detected aside from that, blocking synchronization is only used for proxy creation and initializationString
toString()
A short but useful description of this scope.
-
-
-
Method Detail
-
scope
public <T> Provider<T> scope(Key<T> key, Provider<T> creator)
Provides singleton scope with the following properties:- creates no more than one instance per Key as a creator is used no more than once
- result is cached and returned quickly on subsequent calls
- exception in a creator is not treated as instance creation and is not cached
- creates singletons in parallel whenever possible
- waits for dependent singletons to be created even across threads and when dependencies are shared as long as no circular dependencies are detected
- returns circular proxy only when circular dependencies are detected
- aside from that, blocking synchronization is only used for proxy creation and initialization
- Specified by:
scope
in interfaceScope
- Parameters:
key
- binding keycreator
- locates an instance when one doesn't already exist in this scope.- Returns:
- a new provider which only delegates to the given unscoped provider when an instance of the requested object doesn't already exist in this scope
- See Also:
CycleDetectingLock.CycleDetectingLockFactory
-
-