Support for Java developers

Koin features for Java developers

Below is a short description of koin-java features. This project is a small subset of static utils to help java developers.

Start Koin

Just use the startKoin() static function (with the static import to reduce the syntax):

import static org.koin.core.context.GlobalContext.start;

// Build KoinApplication instance
// Builder API style
KoinApplication koinApp = KoinApplication.create()
                .printLogger()
                .modules(koinModule);

// Statr KoinApplication instance
start(koinApp);

You have access to the same options than the Kotlin startKoin().

Declare a Java friendly module in Kotlin

You need to tag your module variable with @JvmField to make it readable from Java world:

@JvmField
val koinModule = module {
    single { ComponentA() }
    single { ComponentB(get()) }
    single { ComponentC(get(), get()) }

    module("anotherModule") {
        single { ComponentD(get()) }
    }
}

Inject with static helpers

The KoinJavaComponent class is a static helper that brings Koin powers to Java:

  • inject() - lazy inject instance
  • get() - retrieve instance
  • getKoin() - get Koin context
import static org.koin.java.standalone.KoinJavaComponent.*;

ComponentA a = get(ComponentA.class);
Lazy<ComponentA> lazy_a = inject(ComponentA.class);

More about Java features