SkillAgentSearch skills...

Inject

Java JSR 330 dependency injection library

Install / Use

/learn @SuppieRK/Inject
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Inject

Build status Maven Central Javadoc SonarCloud Quality Gate SonarCloud Coverage SonarCloud Maintainability

The work on this software project is in no way associated with my employer nor with the role I'm having at my employer.

I maintain this project alone and as much or as little as my spare time permits using my personal equipment.

Small dependency injection library for Java supporting reflection-based objects creation (aka a factory pattern) using slightly extended JSR 330 specification provided by Jakarta Inject library.

The main goal of this library is to provide the most obvious path to register and reuse dependencies in your code, which was inspired by other libraries such as:

  • Feather - with some features absent here, namely injection into the current class for testing.
  • Dagger 2 - the concept of @Provides is useful, code generation not so much.
  • Guice - without the bloat of additional annotations you might never use or support for Jakarta's servlet,persistence.

In addition to JSR 330 annotations, this library adds its own @Provides annotation to mark methods which produce dependencies for other classes - something that is missing from the specification to let users denote which specific methods outputs should be exposed to other classes.

How to add?

Starting from 3.0.0 this library uses Java 17

| Inject Version | Java | Jakarta Inject | Notes | |----------------|------|----------------|--------------------------------------------| | 3.0.x | 17+ | 2.0.x | Current branch, Java module system enabled |

  • Maven

<dependency>
    <groupId>io.github.suppierk</groupId>
    <artifactId>inject</artifactId>
    <version>3.0.1</version>
</dependency>
  • Gradle (works for both Groovy and Kotlin)
implementation("io.github.suppierk:inject:3.0.1")

Hello, World!

import jakarta.inject.Inject;
import io.github.suppierk.inject.Provides;
import io.github.suppierk.inject.Injector;

public class HelloWorld {
    static class Provider {
        @Provides
        String helloWorld() {
            return "Hello, World!";
        }
    }

    static class Consumer implements Runnable {
        private final String helloWorld;

        @Inject
        public Consumer(String helloWorld) {
            this.helloWorld = helloWorld;
        }

        @Override
        public void run() {
            System.out.println(helloWorld);
        }
    }

    public static void main(String[] args) {
        final Injector injector = Injector.injector()
                .add(Provider.class, Consumer.class)
                .build();

        injector.get(Consumer.class).run();
    }
}

Core concepts

  • Bindings – call Injector.injector().add(...) with classes or instances. Constructors marked with @Inject (or the default constructor) become injectable bindings.
  • Injection – dependencies are resolved via constructor or field injection. Collections and maps are intentionally unsupported; explicit providers keep resolution deterministic.
  • @Provides methods – factory methods on registered classes expose additional bindings. Annotate with @Singleton to memoize results.
  • Scopes – classes or @Provides return types marked with @Singleton produce a single memoized instance. Others are created on demand.
  • Error handling – missing or ambiguous bindings raise IllegalArgumentException at build time; runtime lookups throw NoSuchElementException. Injector.toString() prints the graph in YAML for debugging.
  • Circular dependencies – direct cycles fail fast with Found cycle errors. Break loops by injecting Provider<T>/ Supplier<T> on at least one edge, or restructure into singleton factories.

Known problems

  • Do not invoke Provider.get() or Supplier.get() in the constructor of the object which is a part of the dependency cycle - this will result in an infinite instantiation loop and will cause your program to become stuck.

More examples

  • Visit our Wiki for more in-depth look at the features.
  • Take a look at the complete stack proposal in JIQS repository.

Contributing locally

./gradlew spotlessApply           # Format sources
./gradlew --no-daemon spotlessCheck build # CI parity fast path
./gradlew --no-daemon pitest      # Mutation testing (slow, run before PRs)

Related Skills

View on GitHub
GitHub Stars14
CategoryDevelopment
Updated1d ago
Forks0

Languages

Java

Security Score

95/100

Audited on Apr 2, 2026

No findings