Closed antohaby closed 4 years ago
Describe the bug When using koin as Ktor feature beans that defined with "createAtStart=true" attribute doesn't actually start.
To Reproduce
createdAtStart=true
println
Expected behavior Bean has to be created at the start of Ktor application
Koin project used and used version (please complete the following information): koin-core version 2.0.1 koin-ktor version 2.0.1
koin-core version 2.0.1
koin-ktor version 2.0.1
Cause Installation function (https://github.com/InsertKoinIO/koin/blob/7cd226d85b6d4efce90d16f8bd717792cdae87db/koin-projects/koin-ktor/src/main/kotlin/org/koin/ktor/ext/KoinFeature.kt#L37) doesn't trigger createEagerInstances
createEagerInstances
As for example GlobalContext.startKoin (https://github.com/InsertKoinIO/koin/blob/master/koin-projects/koin-core/src/main/kotlin/org/koin/core/context/GlobalContext.kt#L69) does
GlobalContext.startKoin
Workaround Call createEagerInstances() during installation
createEagerInstances()
install(Koin) { /*...*/ modules(/*...*/) // Call after modules are set createEagerInstances() }
Possible fix In Koin.install instead GlobalContext.start(application) use GlobalContext.startKoin(configure)
Koin.install
GlobalContext.start(application)
GlobalContext.startKoin(configure)
class Koin { /* ... */ override fun install(pipeline: Application, configure: KoinAppDeclaration): Koin { startKoin(configure) pipeline.environment.monitor.subscribe(ApplicationStopping) { GlobalContext.stop() } return Koin() } } }
This bug is fixed at https://github.com/InsertKoinIO/koin/pull/548
Let's continue there #548 then 👍
Describe the bug When using koin as Ktor feature beans that defined with "createAtStart=true" attribute doesn't actually start.
To Reproduce
createdAtStart=true
and callprintln
for example inside the body.Expected behavior Bean has to be created at the start of Ktor application
Koin project used and used version (please complete the following information):
koin-core version 2.0.1
koin-ktor version 2.0.1
Cause Installation function (https://github.com/InsertKoinIO/koin/blob/7cd226d85b6d4efce90d16f8bd717792cdae87db/koin-projects/koin-ktor/src/main/kotlin/org/koin/ktor/ext/KoinFeature.kt#L37) doesn't trigger
createEagerInstances
As for example
GlobalContext.startKoin
(https://github.com/InsertKoinIO/koin/blob/master/koin-projects/koin-core/src/main/kotlin/org/koin/core/context/GlobalContext.kt#L69) doesWorkaround Call
createEagerInstances()
during installationPossible fix In
Koin.install
insteadGlobalContext.start(application)
useGlobalContext.startKoin(configure)