@ThreadSafe public class DefaultInjector extends Object implements Injector
DefaultInjector is the default Injector implementation. When resolving the
dependency graph for a desire, a "context" is built which consists of an
ordering of qualified types that satisfy each dependency. The DefaultInjector
uses the DependencySolver
to manage dependency resolution. New
injectors can easily be built to also use this solver.
Constructor and Description |
---|
DefaultInjector(BindingFunction... functions)
Create a new DefaultInjector.
|
DefaultInjector(CachePolicy defaultPolicy,
BindingFunction... functions)
Create a new DefaultInjector.
|
DefaultInjector(CachePolicy defaultPolicy,
int maxDepth,
BindingFunction... functions)
Create a new DefaultInjector.
|
Modifier and Type | Method and Description |
---|---|
<T> T |
getInstance(Annotation qualifier,
Class<T> type)
Get an instance of T with the given
Qualifier annotation. |
<T> T |
getInstance(Class<T> type)
Get an instance of T based on the bindings that this Injector was
configured with.
|
DependencySolver |
getSolver() |
public DefaultInjector(BindingFunction... functions)
Create a new DefaultInjector. The created resolver will use a max dependency depth of 100 to estimate if there are cycles in the dependency hierarchy. Bindings with a NO_PREFERENCE cache policy will be treated as NEW_INSTANCE.
functions
- The BindingFunctions to use, ordered with highest
priority function firstNullPointerException
- if spi or functions ar enullpublic DefaultInjector(CachePolicy defaultPolicy, BindingFunction... functions)
Create a new DefaultInjector. The created resolver will use a max dependency depth of 100 to estimate if there are cycles in the dependency hierarchy. Bindings with a NO_PREFERENCE cache policy will use defaultPolicy.
defaultPolicy
- The CachePolicy used in place of NO_PREFERENCEfunctions
- The BindingFunctions to use, ordered with highest
priority functions firstIllegalArgumentException
- if defaultPolicy is NO_PREFERENCENullPointerException
- if spi or functions are nullpublic DefaultInjector(CachePolicy defaultPolicy, int maxDepth, BindingFunction... functions)
Create a new DefaultInjector. maxDepth represents the maximum depth of the dependency hierarchy before it is assume that there is a cycle. Bindings with a NO_PREFERENCE cache policy will use defaultPolicy.
This constructor can be used to increase this depth in the event that configuration requires it, although for most purposes the default 100 should be sufficient.
defaultPolicy
- The CachePolicy used in place of NO_PREFERENCEmaxDepth
- The maximum depth of the dependency hierarchyfunctions
- The BindingFunctions to use, ordered with highest
priority functions firstIllegalArgumentException
- if maxDepth is less than 1, or if
defaultPolicy is NO_PREFERENCENullPointerException
- if spi or functions are nullpublic DependencySolver getSolver()
public <T> T getInstance(Class<T> type) throws InjectionException
Injector
Get an instance of T based on the bindings that this Injector was configured with. An exception is thrown if the request type cannot be instantiated with dependency injection.
Injectors may memoize or cache previously created objects. As an example,
the Injector created by InjectorBuilder
reuses instances where
possible.
getInstance
in interface Injector
T
- The object type being createdtype
- The class typeConstructionException
- if type cannot be instantiatedInjectionException
public <T> T getInstance(Annotation qualifier, Class<T> type) throws InjectionException
Injector
Get an instance of T with the given Qualifier
annotation.
getInstance
in interface Injector
T
- The object typequalifier
- The qualifier on of the returned instancetype
- The class typeConstructionException
- if type cannot be instantiatedInjectionException
Copyright © 2014 GroupLens Research. All Rights Reserved.