Top Description Methods
com.google.inject

public Interface Injector

Known Direct Implementers
com.google.inject.internal.InternalInjectorCreator.ToolStageInjector, com.google.inject.internal.InjectorImpl
Imports
com.google.inject.spi.Element, .InjectionPoint, .TypeConverterBinding, java.lang.annotation.Annotation, java.util.List, .Map, .Set

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.

Authors
[email protected] (Bob Lee), [email protected] (Jesse Wilson)

Method Summary

Modifier and TypeMethod and Description
public Injector
createChildInjector(Iterable<? extends Module> modules)

Returns a new injector that inherits all state from this injector.

public Injector
createChildInjector(Module... modules)

Returns a new injector that inherits all state from this injector.

public <T> List<Binding<T>>
findBindingsByType(TypeLiteral<T> type)

Returns all explicit bindings for type.

public Map<Key<?>, Binding<?>>
getAllBindings()

Returns a snapshot of this injector's bindings, both explicit and just-in-time.

public Map<TypeLiteral<?>, List<InjectionPoint>>
getAllMembersInjectorInjectionPoints()

Returns the injection points created for calls to getMembersInjector (either directly or indirectly, e.g. through injectMembers.

public <T> Binding<T>
getBinding(Key<T> key)

Returns the binding for the given injection key.

public <T> Binding<T>
getBinding(Class<T> type)

Returns the binding for the given type.

public Map<Key<?>, Binding<?>>
getBindings()

Returns this injector's explicit bindings.

public List<Element>
getElements()

Returns the elements that make up this injector.

public <T> Binding<T>
getExistingBinding(Key<T> key)

Returns the binding if it already exists, or null if does not exist.

public <T> T
getInstance(Key<T> key)

Returns the appropriate instance for the given injection key; equivalent to getProvider(key).get().

public <T> T
getInstance(Class<T> type)

Returns the appropriate instance for the given injection type; equivalent to getProvider(type).get().

public <T> MembersInjector<T>
getMembersInjector(TypeLiteral<T>
type to get members injector for
typeLiteral
)

Returns the members injector used to inject dependencies into methods and fields on instances of the given type T.

public <T> MembersInjector<T>
getMembersInjector(Class<T>
type to get members injector for
type
)

Returns the members injector used to inject dependencies into methods and fields on instances of the given type T.

public Injector
getParent()

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

public <T> Provider<T>
getProvider(Key<T> key)

Returns the provider used to obtain instances for the given injection key.

public <T> Provider<T>
getProvider(Class<T> type)

Returns the provider used to obtain instances for the given type.

public Map<Class<? extends Annotation>, Scope>
getScopeBindings()

Returns a map containing all scopes in the injector.

public Set<TypeConverterBinding>
getTypeConverterBindings()

Returns a set containing all type converter bindings in the injector.

public void
injectMembers(Object
to inject members on
instance
)

Injects dependencies into the fields and methods of instance.

Method Detail

createChildInjectorback to summary
public 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. 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.

When using hierarchical injectors (via Binder#newPrivateBinder, Binder#PrivateModule, or Injector#createChildInjector), extra care is required to ensure that bindings are associated with the correct injector. Statements that are otherwise unnecessary - such as {code bind(ServiceImpl.class);} - become critical to associate the binding with this particular injector, otherwise Guice may promote the binding to an ancestor injector. Linked bindings such as bind(Service.class).to(ServiceImpl.class); will only associate the Service binding with the current injector; ServiceImpl will still be promoted to an ancestor injector without an explicit bind(ServiceImpl.class); statement.

Since
2.0
createChildInjectorback to summary
public 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
findBindingsByTypeback to summary
public <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.

getAllBindingsback to summary
public Map<Key<?>, Binding<?>> getAllBindings()

Returns a snapshot of this injector's bindings, both explicit and just-in-time. The returned map is immutable; it contains only the bindings that were present when getAllBindings() was invoked. Just-in-time bindings are only present if they have been requested at least once. 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.

Since
3.0
getAllMembersInjectorInjectionPointsback to summary
public Map<TypeLiteral<?>, List<InjectionPoint>> getAllMembersInjectorInjectionPoints()

Returns the injection points created for calls to getMembersInjector (either directly or indirectly, e.g. through injectMembers.

This excludes any injection points from elements (which are accessible from each element via the SPI), unless getMembersInjector or injectMembers were also called for the same key.

The returned multimap does not include data 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.

Since
4.2.3
getBindingback to summary
public <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.

Exceptions
ConfigurationException:
if this injector cannot find or create the binding.
getBindingback to summary
public <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.

Exceptions
ConfigurationException:
if this injector cannot find or create the binding.
Since
2.0
getBindingsback to summary
public Map<Key<?>, Binding<?>> getBindings()

Returns this injector's 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.

getElementsback to summary
public List<Element> getElements()

Returns the elements that make up this injector. Note that not all kinds of elements are returned.

The returned list does not include elements inherited from a parent injector, should one exist.

The returned list is immutable; it contains only the elements that were present when getElements was invoked. Subsequent calls may return a list with additional elements.

The returned list does not include data 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.

Since
4.2.3
getExistingBindingback to summary
public <T> Binding<T> getExistingBinding(Key<T> key)

Returns the binding if it already exists, or null if does not exist. Unlike 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.

Since
3.0
getInstanceback to summary
public <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.

Exceptions
ConfigurationException:
if this injector cannot find or create the provider.
ProvisionException:
if there was a runtime failure while providing an instance.
getInstanceback to summary
public <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.

Exceptions
ConfigurationException:
if this injector cannot find or create the provider.
ProvisionException:
if there was a runtime failure while providing an instance.
getMembersInjectorback to summary
public <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:TypeLiteral<T>

type to get members injector for

Since
2.0
See Also
{@link Binder#getMembersInjector(TypeLiteral)} for an alternative that offers up front error detection
getMembersInjectorback to summary
public <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:Class<T>

type to get members injector for

Since
2.0
See Also
{@link Binder#getMembersInjector(Class)} for an alternative that offers up front error detection
getParentback to summary
public Injector getParent()

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

Since
2.0
getProviderback to summary
public <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.

Exceptions
ConfigurationException:
if this injector cannot find or create the provider.
See Also
{@link Binder#getProvider(Key)} for an alternative that offers up front error detection
getProviderback to summary
public <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.

Exceptions
ConfigurationException:
if this injector cannot find or create the provider.
See Also
{@link Binder#getProvider(Class)} for an alternative that offers up front error detection
getScopeBindingsback to summary
public Map<Class<? extends Annotation>, Scope> getScopeBindings()

Returns a map containing all scopes in the injector. The maps keys are scoping annotations like 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.

Since
3.0
getTypeConverterBindingsback to summary
public Set<TypeConverterBinding> getTypeConverterBindings()

Returns a set containing all type converter bindings in the injector. The returned set is immutable.

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

Since
3.0
injectMembersback to summary
public 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:Object

to inject members on

See Also
{@link Binder#getMembersInjector(Class)} for a preferred alternative that supports checks before run time