A lightweight and efficient Dependency Injection (DI) library for Java applications, designed for simplicity and flexibility.
- Module-Based Dependency Management (Static and Dynamic Modules)
This library is designed to be minimalistic and does not support the following features:
- Supertype Injections: Beans must be explicitly defined with their concrete types; injection by supertype or interface is not supported.
- Cyclic Dependencies: The library does not support cyclic dependencies. If a cycle is detected, an exception is thrown with no way to resolve it.
Add the following dependency to your pom.xml
if using Maven:
<dependency>
<groupId>com.github.tix320</groupId>
<artifactId>ravel</artifactId>
<version>0.4.0</version>
</dependency>
For Gradle:
dependencies {
implementation 'com.github.tix320:ravel:0.4.0'
}
The library is structured around modules, which can be either dynamic or static. Each module contains a list of bean definitions.
- Bean Key: A combination of the bean type and its qualifier.
- Scope: Either
SINGLETON
(one instance per injector) orPROTOTYPE
(a new instance per injection point). - Dependencies: Other beans, represented by their Bean Keys.
- Factory Method: A function that accepts dependencies and creates an instance of the bean.
- Static Modules: Defined declaratively by a class with annotated factory methods.
- Dynamic Modules: Built programmatically, allowing logic-driven bean definitions rather than declarative configurations.
Static and dynamic modules can be used together within the same injector.
@RequireModules(statics = {ModuleB.class})
public class ModuleA {
@Singleton
public BeanA a(BeanB b) {
return new BeanA(b);
}
}
public class ModuleB {
@Singleton
public BeanB b() {
return new BeanB();
}
}
var beanDefinition = new BeanDefinition(
new BeanKey(BeanB.class), Scope.SINGLETON,
List.of(), dependencies -> new BeanB()
);
DynamicModuleDefinition dynamicModule = new DynamicModuleDefinition(
"dynamic", List.of(), List.of(beanDefinition)
);
Injector injector = new Injector();
injector.registerModule(ModuleA.class);
injector.registerDynamicModule(dynamicModule);
injector.build();
BeanA beanA = injector.inject(BeanA.class);
This project follows Semantic Versioning.
Ravel is released under the Apache License, Version 2.0.
- Author: tix320