Starting Koin

Running your modules

One you have declared your dependencies with the Koin DSL, you just need to start Koin and inject!

The start function

The startKoin() function just need a list of Module to run and it will load every defintion from your modules. It is a function, it can be used every where, dependening on the used runtime.

startKoin(listOf(myModule))

Properties

External properties can be loaded while starting your modules.

Load from koin.properties

Koin will check if koin.properties is available, to laod your properties. It has to be in src/main/resources Kotlin folder or asset Android folder.

External properties

For koin core/spark/ktor, you can also load environment properties with the bindSystemProperties parameter:

startKoin(listOf(myModule),bindSystemProperties = true)

Additional properties

You can also supply properties on the fly, with the Koin start function with the properties attribute:

startKoin(listOf(myModule),properties = HashMap(...properties))

Starting Koin

Depending on your choosen Koin module, Koin will be launched slight differently.

Kotlin

For a Kotlin app, just start it in your main function:

fun main(vararg args: String) {
    start(listOf(myModule))
}

Android

For an Android app, you will start Koin in your Application class:

class MyApplication : Application(){
    override fun onCreate() {
        super.onCreate()
        // Start Koin
        startKoin(this, listOf(myModule))
    }
}

Spark

For a Spark web app, just run it from your main function:

fun main(vararg args: String) {
    // Spark
    start(modules = listOf(myModule)) {
        // Controllers
        runControllers()
    }
}

And now?

Your dependencies are delcared in modules. You have started Koin. Let’s go with injection!