Trew is a very lightweight and fast dependency injection library based on Guice like the old Syringe DI. Trew, unlike Syringe, supports circular dependencies, this is an important difference between these D.I
Trew helps you create a very scalable and orderly application in Java, check this examples with Trew and without Trew
Injector injector = Injector.create(binder -> {
binder.bind(Tea.class).toProvider(TeaProvider.class).singleton();
binder.bind(Cookie.class).toInstance(new ChocolateCookie());
});
public class TeaProvider implements Provider<Tea> {
@Override
public Tea get() {
Season season = Season.current();
if (season == Season.SUMMER || season == Season.SPRING) {
return new IcedTea();
} else {
return new HotTea();
}
}
}
public class Consumer {
@Inject private Tea tea;
@Inject private Cookie cookie;
public void consumeYourStuff() {
// do something with tea and cookie...
}
}
public class TeaFactory {
private static final Object lock = new Object();
private static Tea tea;
public static Tea get() {
if (tea == null) {
// thread-safety
synchronized (lock) {
if (tea == null) {
Season season = Season.current();
if (season == Season.SUMMER || season == Season.SPRING) {
tea = new IcedTea();
} else {
tea = new HotTea();
}
}
}
}
return tea;
}
}
public class Consumer {
private final Tea tea;
private final Cookie cookie;
public Consumer(Tea tea, Cookie cookie) {
// trew checks nullability for you, if an injectable is annotated with
// any @Nullable annotation, it's handled as an optional injection
this.tea = Objects.requireNonNull(tea);
this.cookie = Objects.requireNonNull(cookie);
}
public void consumeYourStuff() {
// do something with tea and cookie...
}
}
Instantiation without Trew is new Consumer(TeaFactory.get(), new ChocolatCookie());
Instantiation with Trew is Injector#getInstance(Consumer.class)
or just @Inject private Consumer consumer;
, with Trew the implementations are only known by the configuration Module
s
Read the Wiki on GitHub to learn about the usage of Trew
You can simply download the JAR from GitHub in the Releases section, or use Maven (recommended)
Releases repository, all versions that doesn't end with "-SNAPSHOT" will be here.
<repository>
<id>unnamed-releases</id>
<url>https://repo.unnamed.team/repository/unnamed-releases/</url>
</repository>
Snapshots repository, all versions ending with "-SNAPSHOT" will be here
<repository>
<id>unnamed-snapshots</id>
<url>https://repo.unnamed.team/repository/unnamed-snapshots/</url>
</repository>
Put this in your POM.xml <dependencies>
section
<dependency>
<groupId>me.yushust.inject</groupId>
<artifactId>trew</artifactId>
<version>0.2.5-SNAPSHOT</version> <!--Check the latest version in the repositories-->
</dependency>