/okaeri-injector

Probably the most basic and simplest DI possible

Primary LanguageJavaMIT LicenseMIT

Okaeri Injector

License Total lines Repo size Contributors Discord

Probably the most basic and simplest DI possible with just ~15kB in size. Part of the okaeri-platform.

Installation

Maven

Add repository to the repositories section:

<repository>
    <id>okaeri-repo</id>
    <url>https://storehouse.okaeri.eu/repository/maven-public/</url>
</repository>

Add dependency to the dependencies section:

<dependency>
  <groupId>eu.okaeri</groupId>
  <artifactId>okaeri-injector</artifactId>
  <version>2.1.0</version>
</dependency>

Gradle

Add repository to the repositories section:

maven { url "https://storehouse.okaeri.eu/repository/maven-public/" }

Add dependency to the maven section:

implementation 'eu.okaeri:okaeri-injector:2.1.0'

Example

More complex example can be found in the tests.

public class Worker {

    @Inject
    // @Inject("byName")
    private Api api;

    @PostConstruct
    private void initialize() {
        System.out.println("running api call");
        this.api.test();
    }
}
public final class TestInjector {

    public static void main(String[] args) {

        Injector injector = OkaeriInjector.create()
                // .registerInjectable("byName", new Api())
                .registerInjectable(new Api());

        Worker worker = injector.createInstance(Worker.class);
        System.out.println(worker);
    }
}
public class Api {

    public void test() {
        System.out.println("Hello World!");
    }
}