imgn inject
imgn inject is a dependency injection framework for Java 21 or later that uses
no reflection: an annotation processor generates a factory for each bean at
compile time, and the injector finds those factories through ServiceLoader,
on the class path and on the module path. Every class is documented in the
API documentation.
Artifacts
| Artifact | Module | What it does |
|---|---|---|
inject |
be.imgn.inject |
The annotations and the injector |
inject-processor |
be.imgn.inject.processor |
The annotation processor, and its plugin SPI |
inject-maven-plugin |
Registers the generated factories in module-info.class |
Setting up
Depend on inject, and run inject-processor as an annotation processor:
<dependencies>
<dependency>
<groupId>be.imgn.inject</groupId>
<artifactId>inject</artifactId>
<version>${inject.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>be.imgn.inject</groupId>
<artifactId>inject-processor</artifactId>
<version>${inject.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
The processor claims no annotation, so a build compiling with
-Xlint:all -Werror needs -Xlint:all,-processing (see
injection). On the module path, require
be.imgn.inject and declare the generated factories, by hand or with the
Maven plugin (see module path).
Features
One page per feature. Each page states the problem the feature solves, how it solves it, examples per use case, and its limitations.
Declaring beans
- Zero-reflection injection —
@Injectconstructors, methods, and fields, wired by compile-time generated factories. @Providesmethods — bindings for types you do not own or that need assembly logic.- Qualifiers — distinguishing bindings of the same type;
@Namedand custom qualifier annotations. - Type exposure and
@ExposeAs— which types a bean is injectable as. - Scopes — prototype by default,
@Singleton, and the custom scope SPI. - Conditional beans — beans present only when a
condition matches;
@ConditionalOnPropertyand the condition SPI. - Lifecycle callbacks —
@PostConstructafter full injection and@PreDestroyon injector close. - Construction listeners —
ConstructionListenercontributions called with every instance the container builds. - Eager singletons —
@Eagersingletons built when the root injector is created. - Method interception —
@InterceptingAnnotationtriggers and@Interceptsbeans running around method calls, wired by a generated subclass.
Consuming beans
Provider<T>injection — deferred and repeated resolution.- Keys and generic types —
Key,TypeLiteral, andTypesfor qualified and parameterized lookups. - Multibindings —
@IntoSet/@IntoMapcontributions aggregated into injectableSetandMapbindings. - Assisted injection — factories mixing container dependencies with caller-supplied values.
- Injector injection and introspection — resolving
through the
Injectoritself; probing and enumerating bean definitions.
Structuring graphs
- Child injectors — request- or tenant-scoped binding layers over a shared parent graph, and child-local scopes cached per child.
- Builder overrides — replacing discovered bindings with explicit instances or providers, primarily for tests.
Safety and tooling
- Cycle detection — compile-time rejection plus a runtime guard with readable cycle paths.
- Processor plugins — site-derived injected values via
the
@InjectingAnnotation/InjectPluginSPI. - Classpath and module path support —
ServiceLoaderdiscovery in both worlds, and the Maven plugin for module descriptors.