Skip to content

manasb-uoe/java-ioc-container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

java-ioc-container

An Inversion of Control (IoC) container for Java.

How to use?

Getting an instance of the Container:

var container = Container.getInstance();

Registering instances by name:

container.registerInstance("random string", "key");
var resolved = container.<String>resolve("key");

Registering instances by type:

container.registerInstance("hello world");
var resolved = container.resolve(String.class);

Registering by type:

 container.registerType(ITestInterface.class, TestConcreteClass.class);
 var resolved = container.resolve(ITestInterface.class);

Registering singletons by type:

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);

About

An inversion of Control (IoC) container for Java

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages