|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Injector
Builds the graphs of objects that make up your application. The injector tracks the dependencies for each type and uses bindings to inject them. This is the core of Guice, although you rarely interact with it directly. This "behind-the-scenes" operation is what distinguishes dependency injection from its cousin, the service locator pattern.
Contains several default bindings:
Injector
instance itself
Provider<T>
for each binding of type T
Logger
for the class being injected
Stage
in which the Injector was created
Guice
.
An injector can also inject the dependencies
of
already-constructed instances. This can be used to interoperate with objects created by other
frameworks or services.
Injectors can be hierarchical
. Child injectors inherit
the configuration of their parent injectors, but the converse does not hold.
The injector's internal bindings
are available for introspection. This
enables tools and extensions to operate on an injector reflectively.
Method Summary | ||
---|---|---|
Injector |
createChildInjector(Iterable<? extends Module> modules)
Returns a new injector that inherits all state from this injector. |
|
Injector |
createChildInjector(Module... modules)
Returns a new injector that inherits all state from this injector. |
|
|
findBindingsByType(TypeLiteral<T> type)
Returns all explicit bindings for type . |
|
|
getBinding(Class<T> type)
Returns the binding for the given type. |
|
|
getBinding(Key<T> key)
Returns the binding for the given injection key. |
|
Map<Key<?>,Binding<?>> |
getBindings()
Returns all explicit bindings. |
|
|
getInstance(Class<T> type)
Returns the appropriate instance for the given injection type; equivalent to getProvider(type).get() . |
|
|
getInstance(Key<T> key)
Returns the appropriate instance for the given injection key; equivalent to getProvider(key).get() . |
|
Injector |
getParent()
Returns this injector's parent, or null if this is a top-level injector. |
|
|
getProvider(Class<T> type)
Returns the provider used to obtain instances for the given type. |
|
|
getProvider(Key<T> key)
Returns the provider used to obtain instances for the given injection key. |
|
void |
injectMembers(Object instance)
Injects dependencies into the fields and methods of instance . |
Method Detail |
---|
void injectMembers(Object instance)
instance
. Ignores the presence or
absence of an injectable constructor.
Whenever Guice creates an instance, it performs this injection automatically (after first performing constructor injection), so if you're able to let Guice create all your objects for you, you'll never need to use this method.
Map<Key<?>,Binding<?>> getBindings()
The returned map does not include bindings inherited from a parent
injector
, should one exist.
This method is part of the Guice SPI and is intended for use by tools and extensions.
<T> Binding<T> getBinding(Key<T> key)
This method is part of the Guice SPI and is intended for use by tools and extensions.
ConfigurationException
- if this injector cannot find or create the binding.<T> Binding<T> getBinding(Class<T> type)
This method is part of the Guice SPI and is intended for use by tools and extensions.
ConfigurationException
- if this injector cannot find or create the binding.<T> List<Binding<T>> findBindingsByType(TypeLiteral<T> type)
type
.
This method is part of the Guice SPI and is intended for use by tools and extensions.
<T> Provider<T> getProvider(Key<T> key)
ConfigurationException
- if this injector cannot find or create the provider.<T> Provider<T> getProvider(Class<T> type)
ConfigurationException
- if this injector cannot find or create the provider.<T> T getInstance(Key<T> key)
getProvider(key).get()
. When feasible, avoid using this method, in favor of having Guice
inject your dependencies ahead of time.
ConfigurationException
- if this injector cannot find or create the provider.
ProvisionException
- if there was a runtime failure while providing an instance.<T> T getInstance(Class<T> type)
getProvider(type).get()
. When feasible, avoid using this method, in favor of having Guice
inject your dependencies ahead of time.
ConfigurationException
- if this injector cannot find or create the provider.
ProvisionException
- if there was a runtime failure while providing an instance.Injector getParent()
null
if this is a top-level injector.
Injector createChildInjector(Iterable<? extends Module> modules)
Just-in-time bindings created for child injectors will be created in an ancestor injector whenever possible. This allows for scoped instances to be shared between injectors. Use explicit bindings to prevent bindings from being shared with the parent injector.
No key may be bound by both an injector and one of its ancestors. This includes just-in-time
bindings. The lone exception is the key for Injector.class
, which is bound by each
injector to itself.
Injector createChildInjector(Module... modules)
Just-in-time bindings created for child injectors will be created in an ancestor injector whenever possible. This allows for scoped instances to be shared between injectors. Use explicit bindings to prevent bindings from being shared with the parent injector.
No key may be bound by both an injector and one of its ancestors. This includes just-in-time
bindings. The lone exception is the key for Injector.class
, which is bound by each
injector to itself.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |