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 type T
  • 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 Type Method Description
    <T> java.util.List<Binding<T>> findBindingsByType​(TypeLiteral<T> type)
    Returns all explicit bindings for type.
    <T> T getInstance​(java.lang.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​(java.lang.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.
    <T> Provider<T> getProvider​(java.lang.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​(java.lang.Object instance)
    Injects dependencies into the fields and methods of instance.
  • Method Details