Inject
Java JSR 330 dependency injection library
Install / Use
/learn @SuppieRK/InjectREADME
Inject
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
@Providesis 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.
@Providesmethods – factory methods on registered classes expose additional bindings. Annotate with@Singletonto memoize results.- Scopes – classes or
@Providesreturn types marked with@Singletonproduce a single memoized instance. Others are created on demand. - Error handling – missing or ambiguous bindings raise
IllegalArgumentExceptionat build time; runtime lookups throwNoSuchElementException.Injector.toString()prints the graph in YAML for debugging. - Circular dependencies – direct cycles fail fast with
Found cycleerrors. Break loops by injectingProvider<T>/Supplier<T>on at least one edge, or restructure into singleton factories.
Known problems
- Do not invoke
Provider.get()orSupplier.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
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
