Core dependency injection framework for Kotlin multiplatform applications with DSL-based configuration and type-safe dependency resolution.
—
Property injection and management system for configuration values and runtime parameters with type-safe access patterns. Enables configuration-driven dependency injection and property-based application setup.
Global property management for application-wide configuration values with type-safe retrieval and default value support.
/**
* Get property value with default fallback
* @param key - Property key
* @param defaultValue - Default value if property not found
* @return Property value or default
*/
fun <T : Any> Koin.getProperty(key: String, defaultValue: T): T
/**
* Get property value or null if not found
* @param key - Property key
* @return Property value or null
*/
fun <T : Any> Koin.getProperty(key: String): T?
/**
* Set property value
* @param key - Property key
* @param value - Property value
*/
fun Koin.setProperty(key: String, value: Any)
/**
* Delete property
* @param key - Property key to delete
*/
fun Koin.deleteProperty(key: String)Usage Examples:
import org.koin.core.context.GlobalContext
import org.koin.core.context.startKoin
// Setup properties at startup
startKoin {
properties(mapOf(
"database.host" to "localhost",
"database.port" to 5432,
"api.timeout" to 30000,
"feature.enabled" to true
))
modules(appModule)
}
// Access properties in modules
val databaseModule = module {
single<DatabaseConfig> {
val host = getKoin().getProperty<String>("database.host")
val port = getKoin().getProperty("database.port", 5432)
val timeout = getKoin().getProperty("connection.timeout", 5000)
DatabaseConfig(host, port, timeout)
}
}
// Runtime property management
fun updateConfiguration() {
val koin = GlobalContext.get()
// Update property
koin.setProperty("api.timeout", 60000)
// Get updated value
val newTimeout = koin.getProperty("api.timeout", 30000)
// Remove obsolete property
koin.deleteProperty("deprecated.setting")
}Scope-specific property access with inheritance from parent scopes and the global Koin context.
/**
* Get property value with default fallback from scope context
* @param key - Property key
* @param defaultValue - Default value if property not found
* @return Property value or default
*/
fun <T : Any> Scope.getProperty(key: String, defaultValue: T): T
/**
* Get property value or null from scope context
* @param key - Property key
* @return Property value or null
*/
fun <T : Any> Scope.getPropertyOrNull(key: String): T?
/**
* Get property value from scope context (throws if not found)
* @param key - Property key
* @return Property value
* @throws MissingPropertyException if property not found
*/
fun <T : Any> Scope.getProperty(key: String): TUsage Examples:
import org.koin.dsl.module
// Scope with property access
val userModule = module {
scope<UserSession> {
scoped<UserPreferences> {
val theme = getProperty("user.theme", "light")
val language = getProperty<String>("user.language")
val notifications = getProperty("notifications.enabled", true)
UserPreferences(theme, language, notifications)
}
}
}
// Using properties in scoped components
class UserService : KoinScopeComponent {
override val scope: Scope by lazy { getKoin().createScope<UserService>() }
fun getUserSettings(): UserSettings {
val maxItems = scope.getProperty("display.maxItems", 10)
val sortOrder = scope.getPropertyOrNull<String>("display.sortOrder")
return UserSettings(maxItems, sortOrder)
}
}Direct property injection into Kotlin properties using delegation for seamless configuration access.
/**
* Inject property value into mutable property using global Koin context
*/
inline fun <reified T> KMutableProperty0<T>.inject()
/**
* Inject property value into mutable property using specific Koin instance
* @param koin - Koin instance to use for property resolution
*/
inline fun <reified T> KMutableProperty0<T>.inject(koin: Koin)
/**
* Inject property value into mutable property using specific scope
* @param scope - Scope instance to use for property resolution
*/
inline fun <reified T> KMutableProperty0<T>.inject(scope: Scope)Usage Examples:
import org.koin.ext.inject
import org.koin.core.context.startKoin
// Setup application with properties
startKoin {
properties(mapOf(
"app.version" to "1.0.0",
"app.name" to "MyApplication",
"debug.enabled" to false
))
modules(appModule)
}
// Property injection in classes
class ApplicationConfig {
var appVersion: String = ""
var appName: String = ""
var debugEnabled: Boolean = false
init {
::appVersion.inject() // Injects from "appVersion" property
::appName.inject() // Injects from "appName" property
::debugEnabled.inject() // Injects from "debugEnabled" property
}
}
// Property injection with custom Koin instance
class ServiceConfig(private val koin: Koin) {
var serviceUrl: String = ""
var maxRetries: Int = 0
init {
::serviceUrl.inject(koin)
::maxRetries.inject(koin)
}
}
// Property injection with scope
class UserSessionConfig : KoinScopeComponent {
override val scope: Scope by lazy { getKoin().createScope<UserSessionConfig>() }
var sessionTimeout: Long = 0
var autoSave: Boolean = false
init {
::sessionTimeout.inject(scope)
::autoSave.inject(scope)
}
}Use properties to configure dependency definitions dynamically based on runtime configuration.
Usage Examples:
import org.koin.dsl.module
// Property-driven module configuration
val networkModule = module {
single<HttpClient> {
val baseUrl = getProperty<String>("api.baseUrl")
val timeout = getProperty("api.timeout", 30000)
val retries = getProperty("api.retries", 3)
HttpClient {
baseUrl(baseUrl)
timeout(timeout)
retry(retries)
}
}
single<CacheConfig> {
val cacheSize = getProperty("cache.maxSize", 100)
val cacheTtl = getProperty("cache.ttlMinutes", 60)
val cacheEnabled = getProperty("cache.enabled", true)
if (cacheEnabled) {
CacheConfig(cacheSize, cacheTtl)
} else {
CacheConfig.disabled()
}
}
}
// Environment-specific configuration
val environmentModule = module {
single<DatabaseConnection> {
val environment = getProperty("app.environment", "development")
when (environment) {
"production" -> {
val host = getProperty<String>("db.prod.host")
val credentials = getProperty<String>("db.prod.credentials")
ProductionDatabase(host, credentials)
}
"staging" -> {
val host = getProperty<String>("db.staging.host")
StagingDatabase(host)
}
else -> {
LocalDatabase()
}
}
}
}
// Feature flag driven injection
val featureModule = module {
factory<PaymentProcessor> {
val useNewProcessor = getProperty("feature.newPaymentProcessor", false)
if (useNewProcessor) {
NewPaymentProcessor(get())
} else {
LegacyPaymentProcessor(get())
}
}
}/**
* Exception thrown when required property is not found
*/
class MissingPropertyException(msg: String) : Exception(msg)
/**
* Exception thrown when property file cannot be found
*/
class NoPropertyFileFoundException(msg: String) : Exception(msg)Install with Tessl CLI
npx tessl i tessl/maven-io-insert-koin--koin-core-jvm