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.

  • Method Summary

    Modifier and Type
    Method
    Description
    <T> List<Binding<T>>
    Returns all explicit bindings for type.
    <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> 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.
  • Method Details

    • 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:
    • 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.
    • 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.