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:

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.

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.

Author:
crazybob@google.com (Bob Lee), jessewilson@google.com (Jesse Wilson)

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.
<T> List<Binding<T>>
findBindingsByType(TypeLiteral<T> type)
          Returns all explicit bindings for type.
<T> Binding<T>
getBinding(Class<T> type)
          Returns the binding for the given type.
<T> Binding<T>
getBinding(Key<T> key)
          Returns the binding for the given injection key.
 Map<Key<?>,Binding<?>> getBindings()
          Returns all explicit bindings.
<T> T
getInstance(Class<T> type)
          Returns the appropriate instance for the given injection type; equivalent to getProvider(type).get().
<T> T
getInstance(Key<T> key)
          Returns the appropriate instance for the given injection key; equivalent to getProvider(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 type T.
<T> MembersInjector<T>
getMembersInjector(TypeLiteral<T> 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<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 of instance.
 

Method Detail

injectMembers

void injectMembers(Object instance)
Injects dependencies into the fields and methods of 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.

Parameters:
instance - to inject members on
See Also:
for a preferred alternative that supports checks before run time

getMembersInjector

<T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral)
Returns the members injector used to inject dependencies into methods and fields on instances of the given type T.

Parameters:
typeLiteral - type to get members injector for
Since:
2.0
See Also:
for an alternative that offers up front error detection

getMembersInjector

<T> MembersInjector<T> getMembersInjector(Class<T> type)
Returns the members injector used to inject dependencies into methods and fields on instances of the given type T. When feasible, use Binder.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

getBindings

Map<Key<?>,Binding<?>> getBindings()
Returns all explicit bindings.

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.entrySet() 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.


getBinding

<T> Binding<T> getBinding(Key<T> key)
Returns the binding for the given injection key. This will be an explicit bindings if the key was bound explicitly by a module, or an implicit binding otherwise. The implicit binding will be created if necessary.

This method is part of the Guice SPI and is intended for use by tools and extensions.

Throws:
ConfigurationException - if this injector cannot find or create the binding.

getBinding

<T> Binding<T> getBinding(Class<T> type)
Returns the binding for the given type. This will be an explicit bindings if the injection key was bound explicitly by a module, or an implicit binding otherwise. The implicit binding will be created if necessary.

This method is part of the Guice SPI and is intended for use by tools and extensions.

Throws:
ConfigurationException - if this injector cannot find or create the binding.
Since:
2.0

findBindingsByType

<T> List<Binding<T>> findBindingsByType(TypeLiteral<T> type)
Returns all explicit bindings for type.

This method is part of the Guice SPI and is intended for use by tools and extensions.


getProvider

<T> Provider<T> getProvider(Key<T> key)
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

<T> Provider<T> getProvider(Class<T> type)
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

<T> T getInstance(Key<T> key)
Returns the appropriate instance for the given injection key; equivalent to getProvider(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

<T> T getInstance(Class<T> type)
Returns the appropriate instance for the given injection type; equivalent to getProvider(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.

getParent

Injector getParent()
Returns this injector's parent, or null if this is a top-level injector.

Since:
2.0

createChildInjector

Injector createChildInjector(Iterable<? extends Module> modules)
Returns a new injector that inherits all state from this injector. All bindings, scopes, interceptors and type converters are inherited -- they are visible to the child injector. Elements of the child injector are not visible to its parent.

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.

Since:
2.0

createChildInjector

Injector createChildInjector(Module... modules)
Returns a new injector that inherits all state from this injector. All bindings, scopes, interceptors and type converters are inherited -- they are visible to the child injector. Elements of the child injector are not visible to its parent.

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.

Since:
2.0


Copyright © 2009-2012. All Rights Reserved.