Arnaud Giuliani <arnaud.g@insert-koin.io> - and on twitter: @arnogiu :revnumber: 1.0.1 :example-caption!: :imagesdir: images :sourcedir: ../kotlin

Covers Koin features for Spark project.

1. About koin-spark

The koin-spark project is dedicated to bring dependency injection for Spark.

1.1. Gradle setup

Add the koin-ktor dependency to your Gradle project:

// Add Jcenter to your repositories if needed
repositories {
    jcenter()
}
dependencies {
    // Reflection features for Koin
    compile 'org.koin:koin-spark:{revnumber}'
}

1.2. DSL enhancements

The project brings the controller keyword to declare a single instance of your controller:

val helloAppModule = module {
    single { HelloServiceImpl(get()) as HelloService }
    single { HelloRepositoryImpl() as HelloRepository }

    // Declare a controller
    controller { HelloController(get()) }
}

You controller class must extend SparkController interface:

class HelloController(val service: HelloService) : SparkController {
    init {
        get("/hello") {
            service.sayHello()
        }
    }
}

1.3. Spark & Koin

The koin-spark project also introduces start() and stop() function. This first help you start Spark server with Koin modules:

fun main(vararg args: String) {
    // Spark with Koin
    start(modules = listOf(helloAppModule))
}