Class Injector.Builder

java.lang.Object
be.imgn.inject.Injector.Builder
Enclosing interface:
Injector

public static final class Injector.Builder extends Object
Builds an Injector that overlays explicit instance bindings on top of the discovered graph. An override replaces any discovered binding for the same key, including where that key is an injected dependency of another bean, which makes it a convenient seam for substituting fakes in tests.
  • Method Details

    • bind

      public <T> Injector.Builder bind(Class<T> type, T instance)
      Returns this builder, binding instance as the unqualified key for type.
      Type Parameters:
      T - the bound type
      Parameters:
      type - the bound type
      instance - the instance to return for that type
      Returns:
      this builder, binding instance as the unqualified key for type
    • bind

      public <T> Injector.Builder bind(Key<T> key, T instance)
      Returns this builder, binding instance to key.
      Type Parameters:
      T - the bound type
      Parameters:
      key - the binding key
      instance - the instance to return for that key
      Returns:
      this builder, binding instance to key
    • bindProvider

      public <T> Injector.Builder bindProvider(Class<T> type, Provider<T> provider)
      Returns this builder, binding a provider evaluated on every resolution for type.
      Type Parameters:
      T - the bound type
      Parameters:
      type - the bound type
      provider - the provider to consult for that type
      Returns:
      this builder, binding a provider evaluated on every resolution for type
    • bindProvider

      public <T> Injector.Builder bindProvider(Key<T> key, Provider<T> provider)
      Returns this builder, binding a provider evaluated on every resolution for key.
      Type Parameters:
      T - the bound type
      Parameters:
      key - the binding key
      provider - the provider to consult for that key
      Returns:
      this builder, binding a provider evaluated on every resolution for key
    • build

      public Injector build()
      Returns a new injector applying the configured overrides.
      Returns:
      a new injector applying the configured overrides
      Throws:
      UnsatisfiedDependencyException - if a bean depends on a key that neither a discovered factory nor an override binds; the message lists every such dependency. The dependencies of a bean of a child-local scope are checked when a child hosting that scope is built instead.
      RuntimeException - any exception thrown building a construction listener or an Eager singleton, once the singletons already built are destroyed