@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 |
---|---|
void |
close()
Close the injector, shutting down any instantiated components that require shutdown.
|
<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() |
<T> T |
tryGetInstance(Annotation qualifier,
Class<T> type)
Try to get an instance of a component, returning
null if the component
does not have a configured implementation. |
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()
@Nonnull public <T> T getInstance(Class<T> type) throws InjectionException
Injector
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
@Nonnull public <T> T getInstance(Annotation qualifier, Class<T> type) throws InjectionException
Injector
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
@Nullable public <T> T tryGetInstance(Annotation qualifier, Class<T> type) throws InjectionException
Injector
null
if the component
does not have a configured implementation.tryGetInstance
in interface Injector
T
- The component type.qualifier
- The qualifier, or null
for an unqualified component.type
- The component type.T
, or null
if no implemenation of T
is configured.InjectionException
- if there is some other error injecting the instance.public void close()
Injector
close
in interface AutoCloseable
close
in interface Injector
Copyright © 2016 GroupLens Research. All rights reserved.