Interface Injector
- All Known Implementing Classes:
InjectorImpl
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 Summary
Modifier and TypeMethodDescriptionbindImplicit(Class<?> cls) Registers a class for implicit binding.<T> InjectorbindInstance(Class<T> cls, T instance) Binds a specific instance to a class type.bindScope(Class<? extends Annotation> scopeAnnotation, Supplier<Scope> scope) Binds a scope annotation to a supplier that creates scope implementations.bindScope(Class<? extends Annotation> scopeAnnotation, Scope scope) Binds a scope annotation to its implementation.static Injectorcreate()Creates a new Injector instance with default settings.discover(ClassLoader classLoader) Configures the injector to discover injectable components from the specified ClassLoader.<T> TgetInstance(Class<T> key) Retrieves or creates an instance of the specified type.<T> TgetInstance(Key<T> key) Retrieves or creates an instance for the specified key.<T> voidinjectInstance(T instance) Performs field and method injection on an existing instance.
-
Method Details
-
create
-
discover
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 scopescope- 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 scopescope- supplier that creates scope implementations- Returns:
- this injector instance for method chaining
- Throws:
NullPointerException- if either parameter is null
-
bindImplicit
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
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 toinstance- the instance to use for injection- Returns:
- this injector instance for method chaining
- Throws:
NullPointerException- if either parameter is null
-
injectInstance
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
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 nullIllegalStateException- if the type cannot be provided
-
getInstance
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 nullIllegalStateException- if the type cannot be provided
-