admin管理员组文章数量:1406007
I downloaded a ktor setup with koin enabled, but I can't seem to get it work. Not sure if I am doing something wrong or not, as it on the surface looks pretty OK to me.
so my Applikation.kt
file looks like this
fun main(args: Array<String>) {
io.ktor.serverty.EngineMain.main(args)
}
fun Application.module() {
configureFrameworks()
configureSerialization()
configureDatabases()
configureMonitoring()
configureSecurity()
configureRouting()
configureUserRoutes()
}
the configure frameworks function:
fun Application.configureFrameworks() {
install(Koin) {
slf4jLogger()
modules(module {
single<Connection> {
connectToPostgres(embedded = true)
}
single<UserService> {
UserService(get())
}
})
}
}
and configureUserRoutes()
fun Application.configureUserRoutes() {
routing {
val koin = getKoin()
val userService: UserService = koin.get()
get("/login") {
call.respondText("login?")
}
post("/register") {
log.info("[POST] /register")
val newUser = call.receive<User>()
userService.registerUser(newUser)
}
}
}
have I missunderstood something or shouldn't that inject of UserService
work?
I am just getting this error
Exception in thread "main" java.lang.IncompatibleClassChangeError: Found interface io.ktor.server.routing.Routing, but class was expected
at .koin.ktor.ext.RoutingExtKt.getKoin(RoutingExt.kt:74)
I downloaded a ktor setup with koin enabled, but I can't seem to get it work. Not sure if I am doing something wrong or not, as it on the surface looks pretty OK to me.
so my Applikation.kt
file looks like this
fun main(args: Array<String>) {
io.ktor.serverty.EngineMain.main(args)
}
fun Application.module() {
configureFrameworks()
configureSerialization()
configureDatabases()
configureMonitoring()
configureSecurity()
configureRouting()
configureUserRoutes()
}
the configure frameworks function:
fun Application.configureFrameworks() {
install(Koin) {
slf4jLogger()
modules(module {
single<Connection> {
connectToPostgres(embedded = true)
}
single<UserService> {
UserService(get())
}
})
}
}
and configureUserRoutes()
fun Application.configureUserRoutes() {
routing {
val koin = getKoin()
val userService: UserService = koin.get()
get("/login") {
call.respondText("login?")
}
post("/register") {
log.info("[POST] /register")
val newUser = call.receive<User>()
userService.registerUser(newUser)
}
}
}
have I missunderstood something or shouldn't that inject of UserService
work?
I am just getting this error
Exception in thread "main" java.lang.IncompatibleClassChangeError: Found interface io.ktor.server.routing.Routing, but class was expected
at .koin.ktor.ext.RoutingExtKt.getKoin(RoutingExt.kt:74)
Share
asked Mar 6 at 20:38
munHungermunHunger
3,0617 gold badges43 silver badges73 bronze badges
1 Answer
Reset to default 0aha, so I can't use koin inside the routing scope
this works (but not entirely sure why I can't inject inside routing):
fun Application.configureUserRoutes() {
val koin = getKoin()
val userService: UserService = koin.get()
routing {
get("/login") {
call.respondText("login?")
}
post("/register") {
log.info("[POST] /register")
val newUser = call.receive<User>()
userService.registerUser(newUser)
}
}
}
本文标签: kotlinKoin not able to inject serviceStack Overflow
版权声明:本文标题:kotlin - Koin not able to inject service - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744950742a2634067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论