Validation-free, reflection-light dependency injection library for Java.
Injection4j is a lightweight dependency injection container designed for simplicity and performance. Unlike traditional DI frameworks that rely heavily on runtime reflection and classpath scanning (like Spring or Guice), Injection4j focuses on explicit configuration using Java 8+ functional interfaces.
Key Features:
- Zero Configuration Magic: No
@Injectannotation scanning or XML files. - Type-Safe: Uses
Class<T>keys and lambda-based factories. - Reflection-Light: mostly avoids
java.lang.reflectfor instantiation, giving control back to the developer viaSupplierand functional interfaces. - Fast Startup: Ideal for environments where startup time matters.
Add the following dependency to your pom.xml:
<dependency>
<groupId>pt.procurainterna</groupId>
<artifactId>injection4j.core</artifactId>
<version>1.0.0</version>
</dependency>public class MyService {
private final MyRepository repository;
public MyService(MyRepository repository) {
this.repository = repository;
}
}Use MapModuleBuilder to wire your dependencies explicitely:
import pt.procurainterna.injection4j.module.MapModuleBuilder;
import pt.procurainterna.injection4j.module.Module;
Module module = new MapModuleBuilder()
.addValue(MyRepository.class, new MyRepository()) // Singleton value
.addInvocation(MyService.class, MyService::new, MyRepository.class) // Constructor injection
.build();Use RecursiveModuleProvider to resolve the graph:
import pt.procurainterna.injection4j.provider.RecursiveModuleProvider;
RecursiveModuleProvider provider = new RecursiveModuleProvider(module);
MyService service = provider.provide(MyService.class);This project is released under the Unlicense.