Interface Injector

All Superinterfaces:
AutoCloseable

public interface Injector extends AutoCloseable
The user-facing entry point for obtaining instances and providers.

Each create() (or builder() Injector.Builder.build()) call produces a fresh, independent graph: its singletons are created once within that injector and are not shared with any other injector. Discard an injector to discard its singletons.

Prefer constructor injection: annotate a single constructor with @Inject so a bean's dependencies are final, explicit, and available the moment it is constructed. Field and method injection exist for cases constructor injection cannot reach (e.g. framework-managed or cyclic wiring) and leave a window where the bean is only partly initialised.

The injector itself is injectable: any bean may declare an Injector (or Provider<Injector>) dependency and receives the injector that built it. Injector, Context, and Factory can never be bound.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    Builds an Injector that overlays explicit instance bindings on top of the discovered graph.
    static final class 
    Builds a child Injector: explicit instance and provider bindings layered over the parent's graph.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a builder for an injector whose instance bindings shadow discovered ones (a test seam).
    Returns a builder for a child of this injector: its bindings layer over this injector's graph.
    void
    Closes this injector: rejects further resolution through it, including through the providers it returned, closes its live child injectors, newest first, then invokes @PreDestroy on every scoped instance it created, in reverse creation order.
    static Injector
    Returns a new injector backed by all registered factories and scopes.
    Returns the definition bound to key, if any.
    Returns every bean annotated with an annotation carrying stereotype.
    <T> T
    getInstance(Key<T> key)
    Returns an instance for key.
    <T> T
    Returns an instance of the generic type.
    <T> T
    getInstance(Class<T> type)
    Returns an instance of type.
    <T> Provider<T>
    getProvider(Key<T> key)
    Returns the provider for key.
    <T> Provider<T>
    Returns a provider of the generic type.
    <T> Provider<T>
    getProvider(Class<T> type)
    Returns a provider of type.
    Returns a builder for a child of this injector that hosts the child-local scope: its bindings layer over this injector's graph, and it builds and caches the instances of scope's beans resolved through it or its descendants.
  • Method Details

    • getInstance

      <T> T getInstance(Class<T> type)
      Returns an instance of type.
      Type Parameters:
      T - the requested type
      Parameters:
      type - the requested type
      Returns:
      an instance of type
    • getInstance

      <T> T getInstance(Key<T> key)
      Returns an instance for key.
      Type Parameters:
      T - the requested type
      Parameters:
      key - the requested key
      Returns:
      an instance for key
    • getInstance

      <T> T getInstance(TypeLiteral<T> type)
      Returns an instance of the generic type.
      Type Parameters:
      T - the requested type
      Parameters:
      type - the requested type literal
      Returns:
      an instance of the generic type
    • getProvider

      <T> Provider<T> getProvider(Class<T> type)
      Returns a provider of type.
      Type Parameters:
      T - the requested type
      Parameters:
      type - the requested type
      Returns:
      a provider of type
    • getProvider

      <T> Provider<T> getProvider(TypeLiteral<T> type)
      Returns a provider of the generic type.
      Type Parameters:
      T - the requested type
      Parameters:
      type - the requested type literal
      Returns:
      a provider of the generic type
    • getProvider

      <T> Provider<T> getProvider(Key<T> key)
      Returns the provider for key.
      Type Parameters:
      T - the requested type
      Parameters:
      key - the requested key
      Returns:
      the provider for key
    • getBeanDefinition

      Optional<BeanDefinition> getBeanDefinition(Key<?> key)
      Returns the definition bound to key, if any.

      A read-only view for inspecting the graph — the bean's key, declared framework annotations, and injection points — not for building instances.

      Parameters:
      key - the binding key, qualified or not
      Returns:
      the definition bound to key, if any
    • getBeanDefinitionsByStereotype

      List<BeanDefinition> getBeanDefinitionsByStereotype(Class<? extends Annotation> stereotype)
      Returns every bean annotated with an annotation carrying stereotype.

      For example getBeanDefinitionsByStereotype(ScopingAnnotation.class) lists every scoped bean.

      Parameters:
      stereotype - the meta-annotation to filter by
      Returns:
      every bean annotated with an annotation carrying stereotype
    • childBuilder

      Injector.ChildBuilder childBuilder()
      Returns a builder for a child of this injector: its bindings layer over this injector's graph.
      Returns:
      a builder for a child of this injector: its bindings layer over this injector's graph
    • scopedChildBuilder

      Injector.ChildBuilder scopedChildBuilder(Class<? extends Annotation> scope)
      Returns a builder for a child of this injector that hosts the child-local scope: its bindings layer over this injector's graph, and it builds and caches the instances of scope's beans resolved through it or its descendants.

      A scoping annotation is child-local when no Scope service handles it. Each bean of such a scope is created once per hosting child, built through that child, and cached there. Resolving it through an injector with no hosting child in its chain fails with a ScopeResolutionException. Hosting children nest: the nearest one in the chain hosts the bean.

      Parameters:
      scope - the child-local scope the child hosts
      Returns:
      a builder for a child of this injector that hosts scope
    • close

      void close()
      Closes this injector: rejects further resolution through it, including through the providers it returned, closes its live child injectors, newest first, then invokes @PreDestroy on every scoped instance it created, in reverse creation order. A child injector's scoped instances are those of the scope it hosts. When a callback or a child fails, the others still run, and the first failure is thrown with the later ones suppressed. Closing a child never closes its parent. Idempotent.
      Specified by:
      close in interface AutoCloseable
    • create

      static Injector create()
      Returns a new injector backed by all registered factories and scopes.
      Returns:
      a new injector backed by all registered factories and scopes
      Throws:
      UnsatisfiedDependencyException - if a bean depends on a key nothing 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
    • builder

      static Injector.Builder builder()
      Returns a builder for an injector whose instance bindings shadow discovered ones (a test seam).
      Returns:
      a builder for an injector whose instance bindings shadow discovered ones (a test seam)