docs/reference/koin-core/dsl.md
Quick reference for Koin DSL. For detailed guides see Core - Definitions and Core - Modules.
| Approach | Syntax | Package |
|---|---|---|
| Classic DSL | single { Class(get()) } | org.koin.dsl |
| Classic Autowire | singleOf(::Class) | org.koin.dsl |
| Compiler Plugin | single<Class>() | org.koin.plugin.module.dsl |
:::tip The Compiler Plugin DSL provides auto-wiring and compile-time safety. See Compiler Plugin Setup. :::
A KoinApplication instance represents your configured Koin container. This lets you set up logging, load properties, and register modules.
Choose between two approaches:
koinApplication { } - Creates a standalone KoinApplication instancestartKoin { } - Creates a KoinApplication and registers it in the GlobalContext// Standalone instance (useful for testing or custom contexts)
val koinApp = koinApplication {
modules(myModule)
}
// Global instance (standard approach for applications)
startKoin {
logger()
modules(myModule)
}
Within koinApplication or startKoin, you can use:
logger() - Set the logging level and Logger implementation (default: EmptyLogger)modules() - Load modules into the container (accepts list or vararg)properties() - Load a HashMap of propertiesfileProperties() - Load properties from a fileenvironmentProperties() - Load properties from OS environment variablescreateEagerInstances() - Instantiate all definitions marked with createdAtStartallowOverride(Boolean) - Enable/disable definition overriding (default: true since 3.1.0)The key difference between koinApplication and startKoin:
startKoin - Registers the container in GlobalContext, making it accessible via KoinComponent, by inject(), and other global APIskoinApplication - Creates an isolated instance you control directly// Global context - standard usage
startKoin {
logger()
modules(appModule)
}
// Later, anywhere in your app:
class MyClass : KoinComponent {
val service: Service by inject() // Uses GlobalContext
}
// Local context - advanced usage (testing, multi-context apps)
val customKoin = koinApplication {
modules(testModule)
}.koin
val service = customKoin.get<Service>() // Use specific instance
A complete Koin setup example:
startKoin {
// Configure logging
logger(Level.INFO)
// Load properties
environmentProperties()
// Declare modules
modules(
networkModule,
databaseModule,
repositoryModule,
viewModelModule
)
// Create eager singletons
createEagerInstances()
}
For comprehensive module and definition documentation, see:
| Definition | Classic Lambda | Classic Autowire | Compiler Plugin |
|---|---|---|---|
| Singleton | single { Class(get()) } | singleOf(::Class) | single<Class>() |
| Factory | factory { Class(get()) } | factoryOf(::Class) | factory<Class>() |
| Scoped | scoped { Class(get()) } | scopedOf(::Class) | scoped<Class>() |
| ViewModel | viewModel { VM(get()) } | viewModelOf(::VM) | viewModel<VM>() |
val myModule = module {
single<Database>()
single<UserRepository>()
factory<UserPresenter>()
}
val appModule = module {
includes(networkModule, databaseModule)
single<AppConfig>()
}
startKoin {
modules(appModule)
}
See Modules - includes() for details.