Package org.elasticsearch.common.inject
Interface Injector
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:
- This
Injector
instance itself - A
Provider<T>
for each binding of typeT
- The
Logger
for the class being injected - The
Stage
in which the Injector was created
Injectors are created using the facade class 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.
-
Method Summary
Modifier and TypeMethodDescriptionfindBindingsByType(TypeLiteral<T> type)
Returns all explicit bindings fortype
.<T> T
getInstance(Class<T> type)
Returns the appropriate instance for the given injection type; equivalent togetProvider(type).get()
.<T> T
getInstance(Key<T> key)
Returns the appropriate instance for the given injection key; equivalent togetProvider(key).get()
.<T> MembersInjector<T>
getMembersInjector(Class<T> type)
Returns the members injector used to inject dependencies into methods and fields on instances of the given typeT
.<T> MembersInjector<T>
getMembersInjector(TypeLiteral<T> typeLiteral)
Returns the members injector used to inject dependencies into methods and fields on instances of the given typeT
.<T> Provider<T>
getProvider(Class<T> type)
Returns the provider used to obtain instances for the given type.<T> Provider<T>
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 ofinstance
.
-
Method Details
-
injectMembers
Injects dependencies into the fields and methods ofinstance
. 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.
- Parameters:
instance
- to inject members on- See Also:
for a preferred alternative that supports checks before run time
-
getMembersInjector
Returns the members injector used to inject dependencies into methods and fields on instances of the given typeT
.- Parameters:
typeLiteral
- type to get members injector for- Since:
- 2.0
- See Also:
for an alternative that offers up front error detection
-
getMembersInjector
Returns the members injector used to inject dependencies into methods and fields on instances of the given typeT
. When feasible, useBinder.getMembersInjector(TypeLiteral)
instead to get increased up front error detection.- Parameters:
type
- type to get members injector for- Since:
- 2.0
- See Also:
for an alternative that offers up front error detection
-
findBindingsByType
Returns all explicit bindings fortype
.This method is part of the Guice SPI and is intended for use by tools and extensions.
-
getProvider
Returns the provider used to obtain instances for the given injection key. When feasible, avoid using this method, in favor of having Guice inject your dependencies ahead of time.- Throws:
ConfigurationException
- if this injector cannot find or create the provider.- See Also:
for an alternative that offers up front error detection
-
getProvider
Returns the provider used to obtain instances for the given type. When feasible, avoid using this method, in favor of having Guice inject your dependencies ahead of time.- Throws:
ConfigurationException
- if this injector cannot find or create the provider.- See Also:
for an alternative that offers up front error detection
-
getInstance
Returns the appropriate instance for the given injection key; equivalent togetProvider(key).get()
. When feasible, avoid using this method, in favor of having Guice inject your dependencies ahead of time.- Throws:
ConfigurationException
- if this injector cannot find or create the provider.ProvisionException
- if there was a runtime failure while providing an instance.
-
getInstance
Returns the appropriate instance for the given injection type; equivalent togetProvider(type).get()
. When feasible, avoid using this method, in favor of having Guice inject your dependencies ahead of time.- Throws:
ConfigurationException
- if this injector cannot find or create the provider.ProvisionException
- if there was a runtime failure while providing an instance.
-