m0jDI a very simple, lightweight dependency injection library for Java, designed to simplify object creation and dependency management. It supports automatic handling of singletons and non-singleton classes using annotations.
- Singleton Support: Automatically manage singleton instances using the
@Singletonannotation. - Constructor Injection: Inject dependencies via constructors annotated with
@Inject. - Field Injection: Inject dependencies into fields annotated with
@Injected.
Add the library to your project using Gradle:
dependencies {
implementation 'me.m0dii:m0jdi:1.0.0'
}- Mark Classes with Annotations
Use the @Singleton annotation to mark classes as singletons. Use the @Inject annotation for fields or
constructors that require dependency injection.
Example:
@Singleton
public class Service {
public void perform() {
System.out.println("Service performed!");
}
}- Set up Your Application
Create an InjectorContainer, register your classes, and use Injector to inject dependencies.
Example:
public class App {
public static void main(String[] args) {
InjectorContainer container = new InjectorContainer();
container.register(Service.class); // Register your classes
Service service = container.resolve(Service.class); // Resolve instance
Injector injector = new Injector(container);
injector.injectDependencies(service); // Inject dependencies if needed
service.perform();
}
}- Constructor Injection
Annotate the constructor with @Inject to automatically inject its dependencies. (Note: You must resolve the class via
the container for constructor injection to work.)
Example:
public class Controller {
private final Service service;
@Inject
public Controller(Service service) {
this.service = service;
}
public void handleRequest() {
service.perform();
}
}
// Usage:
// container.register(Controller.class);
// Controller controller = container.resolve(Controller.class);
// injector.injectDependencies(controller);- Field Injection
Use the @Inject annotation on fields to inject dependencies. After resolving the instance, call
injector.injectDependencies.
Example:
public class UserHandler {
@Inject
private Service service;
public void processUser() {
service.perform();
}
}
// Usage:
// container.register(UserHandler.class);
// UserHandler userHandler = container.resolve(UserHandler.class);
// injector.injectDependencies(userHandler);