public class Injector extends Object
Dependencies may be retrieved explicitly by calling getInstance(java.lang.Class<T>)
; transitive
dependencies will be automatically injected as needed. For a given injector, all calls to getInstance(java.lang.Class<T>)
are idempotent.
Dependencies are identified by an interface or class, and optionally by a name specified with
@Named
.
The injector looks for any instance that has been previously found for the given interface, or
that has been explicitly registered with Injector.Builder.bind(Class, Object)
or Injector.Builder.bind(Key, Object)
. If none is found, the injector searches for an implementing class
from the following sources, in order:
Injector.Builder.bind(Class, Class)
.
Collection
, then its component type is
recursively sought using PluginFinder.findPlugins(Class)
and an array or collection
of those instances is returned.
ServiceLoader
services under the
dependency type (see also PluginFinder.findPlugin(Class)
).
Injector.Builder.bindDefault(Class,
Class)
.
AutoFactory
; see Scopes below).
If no implementing class is found in the injector or any superinjector, an exception is thrown.
Inject
. (If multiple constructors are
@Inject
annotated, the injector will throw an exception.)
AutoFactory
, then a factory object
implementing that interface is created; a new scoped injector is created for every method call to
the factory, with parameter arguments registered on the scoped injector.
Modifier and Type | Class | Description |
---|---|---|
static class |
Injector.Builder |
Builder for
Injector . |
static class |
Injector.Key<T> |
Identifies an injection point.
|
Constructor | Description |
---|---|
Injector() |
Creates a new empty injector.
|
Modifier and Type | Method | Description |
---|---|---|
<T> T |
getInstance(Class<T> type) |
Finds an instance for the given class.
|
Injector.Builder |
newScopeBuilder(ClassLoader classLoader) |
@Nonnull public <T> T getInstance(@Nonnull Class<T> type)
public Injector.Builder newScopeBuilder(ClassLoader classLoader)