public interface Injector
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.
| Modifier and Type | Method and Description |
|---|---|
Injector |
createChildInjector(Iterable
Returns a new injector that inherits all state from this injector.
|
Injector |
createChildInjector(Module
Returns a new injector that inherits all state from this injector.
|
<T> List |
findBindingsByType(TypeLiteral
Returns all explicit bindings for
type.
|
Map |
getAllBindings()
Returns a snapshot of this injector's bindings,
both explicit and just-in-time.
|
<T> Binding |
getBinding(Class
Returns the binding for the given type.
|
<T> Binding |
getBinding(Key
Returns the binding for the given injection key.
|
Map |
getBindings()
Returns this injector's
explicit bindings.
|
<T> Binding |
getExistingBinding(Key
Returns the binding if it already exists, or null if does not exist.
|
<T> T |
getInstance(Class
Returns the appropriate instance for the given injection type; equivalent to
getProvider(type).get().
|
<T> T |
getInstance(Key
Returns the appropriate instance for the given injection key; equivalent to
getProvider(key).get().
|
<T> MembersInjector |
getMembersInjector(Class
Returns the members injector used to inject dependencies into methods and fields on instances of the given type
T.
|
<T> MembersInjector |
getMembersInjector(TypeLiteral
Returns the members injector used to inject dependencies into methods and fields on instances of the given type
T.
|
Injector |
getParent()
Returns this injector's parent, or
null if this is a top-level injector.
|
<T> Provider |
getProvider(Class
Returns the provider used to obtain instances for the given type.
|
<T> Provider |
getProvider(Key
Returns the provider used to obtain instances for the given injection key.
|
Map |
getScopeBindings()
Returns a map containing all scopes in the injector.
|
Set |
getTypeConverterBindings()
Returns a set containing all type converter bindings in the injector.
|
void |
injectMembers(Object
Injects dependencies into the fields and methods of
instance.
|
void injectMembers(Objectinstance)
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.
instance - to inject members on
for a preferred alternative that supports checks before run time
<T> MembersInjector<T> getMembersInjector(TypeLiteral <T> typeLiteral)
T.
typeLiteral - type to get members injector for
for an alternative that offers up front error detection
<T> MembersInjector<T> getMembersInjector(Class <T> type)
T. When feasible, use
Binder.getMembersInjector(TypeLiteral) instead to get increased up front error detection.
type - type to get members injector for
for an alternative that offers up front error detection
Map<Key <?> ,Binding <?>> getBindings()
The returned map does not include bindings inherited from a parent injector, should one exist. The returned map is guaranteed to iterate (for example, with its Map iterator) in the order of insertion. In other words, the order in which bindings appear in user Modules.
This method is part of the Guice SPI and is intended for use by tools and extensions.
Map<Key <?> ,Binding <?>> getAllBindings()
getAllBindings() was invoked. Subsequent calls may return a map with additional just-in-time bindings.
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> Binding<T> getExistingBinding(Key <T> key)
getBinding(Key), this does not attempt to create just-in-time bindings for keys that aren't bound.
This method is part of the Guice SPI and is intended for use by tools and extensions.
<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.
for an alternative that offers up front error detection
<T> Provider<T> getProvider(Class <T> type)
ConfigurationException - if this injector cannot find or create the provider.
for an alternative that offers up front error detection
<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.
InjectorgetParent()
null if this is a top-level injector.
InjectorcreateChildInjector(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. Optional injections in just-in-time bindings (created in the parent injector) may be silently ignored if the optional dependencies are from the child 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.
InjectorcreateChildInjector(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.
Map<Class <? extends Annotation > ,Scope > getScopeBindings()
Singleton.class, and the values are scope instances, such as
Scopes.SINGLETON. The returned map is immutable.
This method is part of the Guice SPI and is intended for use by tools and extensions.
Set<TypeConverterBinding > getTypeConverterBindings()
This method is part of the Guice SPI and is intended for use by tools and extensions.