An Inversion of Control (IoC) container for Java.
var container = Container.getInstance();
container.registerInstance("random string", "key");
var resolved = container.<String>resolve("key");
container.registerInstance("hello world");
var resolved = container.resolve(String.class);
container.registerType(ITestInterface.class, TestConcreteClass.class);
var resolved = container.resolve(ITestInterface.class);
container.registerSingletonType(ITestInterface.class, TestConcreteClass.class);
var resolved = container.resolve(ITestInterface.class);
Using @InjectionConstructor
and @InstanceName
annotations to mark a constructor to be used for injection and resolving constructor params by name, respectively:
public class ClassWithNonEmptyConstructor {
public TestConcreteClass testConcreteClass;
public int anInt;
public ClassWithNonEmptyConstructor(TestConcreteClass testConcreteClass) {
this.testConcreteClass = testConcreteClass;
anInt = -1;
}
@InjectionConstructor
public ClassWithNonEmptyConstructor(TestConcreteClass testConcreteClass, @InstanceName("blaParam") int anInt) {
this.testConcreteClass = testConcreteClass;
this.anInt = anInt;
}
}
container.registerType(ClassWithNonEmptyConstructor.class, ClassWithNonEmptyConstructor.class);
container.registerType(TestConcreteClass.class, TestConcreteClass.class);
container.registerInstance(1, "blaParam");
var resolved = container.resolve(ClassWithNonEmptyConstructor.class);