public interface Injector
Injector uses dependency injection to act as a factory for creating instances
with complex dependencies. A default implementation of Injector can easily be
created by using an InjectorBuilder
:
InjectorBuilder b = new InjectorBuilder(); b.bind(Foo.class).to(Bar.class); b.applyModule(new MyCustomModule()); // other bindings Injector i = b.build(); assert (i.getInstance(Foo.class) instanceof Bar);
Alternatively, BindingFunctionBuilder
and DependencySolver
can be used to create your own Injector implementations.
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.
|
<T> T getInstance(Class<T> type) throws InjectionException
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.
T
- The object type being createdtype
- The class typeConstructionException
- if type cannot be instantiatedInjectionException
<T> T getInstance(Annotation qualifier, Class<T> type) throws ConstructionException, InjectionException
Get an instance of T with the given Qualifier
annotation.
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.