TinyInjector aims to be a very light weight dependency injection library. For that reason it is written in Java and has zero dependencies.
It only uses constructor dependency injection for that.
The class-path is automatically being searched for classes annotated with an annotation of your choice, although they all have to use the same annotation. All matching classes are topologically sorted and instantiated.
TinyInjector is available via JitPack.
In order to use TinyInjector just add this to your build.gradle
:
repositories {
...
maven { url "https://jitpack.io" }
}
dependencies {
compile "com.github.dremme:tiny-injector:0.10.0"
}
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
...
<dependency>
<groupId>com.github.dremme</groupId>
<artifactId>tiny-injector</artifactId>
<version>0.10.0</version>
</dependency>
@Component public class Foo {
public void beep() { ... }
}
@Component public class Bar {
public Bar(Foo foo) { ... }
}
To automatically scan for these classes just call:
import static hamburg.remme.tinyinjector.Injector.scan;
import hamburg.remme.tinyinjector.Component;
public static void main(String[] args) {
scan(Component.class, "my.package");
}
A Map
of singletons is being created as a result.
To retrieve a singleton just call:
import static hamburg.remme.tinyinjector.Injector.scan;
import hamburg.remme.tinyinjector.Component;
public static void main(String[] args) {
scan(Component.class, "my.package");
var foo = retrieve(Foo.class);
foo.beep();
}
For non-singleton instances dependencies can be injected after scanning:
import static hamburg.remme.tinyinjector.Injector.inject;
import static hamburg.remme.tinyinjector.Injector.scan;
import hamburg.remme.tinyinjector.Component;
public static void main(String[] args) {
scan(Component.class, "my.package");
var bar = inject(Bar.class);
}