imgn inject API GitHub

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

Consuming beans

Structuring graphs

Safety and tooling