Interface Injector

All Known Implementing Classes:
InjectorImpl

public interface Injector
The main entry point for Maven's dependency injection framework.

The Injector manages the creation and injection of objects within the Maven build process. It provides both a builder API for configuring the injection behavior and methods for accessing and injecting beans.

Example usage:

 Injector injector = Injector.create()
     .discover(getClass().getClassLoader())
     .bindInstance(Configuration.class, config);

 MyService service = injector.getInstance(MyService.class);
 
Since:
4.0.0
  • Method Details

    • create

      @Nonnull static Injector create()
      Creates a new Injector instance with default settings.
      Returns:
      a new Injector instance
    • discover

      @Nonnull Injector discover(@Nonnull ClassLoader classLoader)
      Configures the injector to discover injectable components from the specified ClassLoader.

      This method scans for classes annotated with injection-related annotations and automatically registers them with the injector.

      Parameters:
      classLoader - the ClassLoader to scan for injectable components
      Returns:
      this injector instance for method chaining
      Throws:
      NullPointerException - if classLoader is null
    • bindScope

      @Nonnull Injector bindScope(@Nonnull Class<? extends Annotation> scopeAnnotation, @Nonnull Scope scope)
      Binds a scope annotation to its implementation.

      This allows custom scopes to be registered with the injector. The scope annotation must be annotated with Scope.

      Parameters:
      scopeAnnotation - the annotation class that defines the scope
      scope - the scope implementation
      Returns:
      this injector instance for method chaining
      Throws:
      NullPointerException - if either parameter is null
    • bindScope

      @Nonnull Injector bindScope(@Nonnull Class<? extends Annotation> scopeAnnotation, @Nonnull Supplier<Scope> scope)
      Binds a scope annotation to a supplier that creates scope implementations.

      Similar to bindScope(Class, Scope) but allows lazy creation of scope implementations.

      Parameters:
      scopeAnnotation - the annotation class that defines the scope
      scope - supplier that creates scope implementations
      Returns:
      this injector instance for method chaining
      Throws:
      NullPointerException - if either parameter is null
    • bindImplicit

      @Nonnull Injector bindImplicit(@Nonnull Class<?> cls)
      Registers a class for implicit binding.

      Implicit bindings allow the injector to create instances of classes without explicit binding definitions. The class must have appropriate injection annotations.

      Parameters:
      cls - the class to register for implicit binding
      Returns:
      this injector instance for method chaining
      Throws:
      NullPointerException - if cls is null
    • bindInstance

      @Nonnull <T> Injector bindInstance(@Nonnull Class<T> cls, @Nonnull T instance)
      Binds a specific instance to a class type.

      This method allows pre-created instances to be used for injection instead of having the injector create new instances.

      Type Parameters:
      T - the type of the instance
      Parameters:
      cls - the class to bind to
      instance - the instance to use for injection
      Returns:
      this injector instance for method chaining
      Throws:
      NullPointerException - if either parameter is null
    • injectInstance

      <T> void injectInstance(@Nonnull T instance)
      Performs field and method injection on an existing instance.

      This method will inject dependencies into annotated fields and methods of the provided instance but will not create a new instance.

      Type Parameters:
      T - the type of the instance
      Parameters:
      instance - the instance to inject dependencies into
      Throws:
      NullPointerException - if instance is null
    • getInstance

      @Nonnull <T> T getInstance(@Nonnull Class<T> key)
      Retrieves or creates an instance of the specified type.
      Type Parameters:
      T - the type to retrieve
      Parameters:
      key - the class representing the type to retrieve
      Returns:
      an instance of the requested type
      Throws:
      NullPointerException - if key is null
      IllegalStateException - if the type cannot be provided
    • getInstance

      @Nonnull <T> T getInstance(@Nonnull Key<T> key)
      Retrieves or creates an instance for the specified key.

      This method allows retrieval of instances with specific qualifiers or generic type parameters.

      Type Parameters:
      T - the type to retrieve
      Parameters:
      key - the key identifying the instance to retrieve
      Returns:
      an instance matching the requested key
      Throws:
      NullPointerException - if key is null
      IllegalStateException - if the type cannot be provided