Ошибка Quarkus Kotlin Inject

Аннотации @Inject для службы, определенной с помощью «@ApplicationScope», не удается внедрить в Kotlin.

"kotlin.UninitializedPropertyAccessException: средство приветствия свойств lateinit не было инициализировано"

Объяснение аналогичного вопроса: «Эта проблема возникает как сочетание того, как Kotlin обрабатывает аннотации, и отсутствия @Target в определении аннотации .... Добавьте @field: xxx»

Вопрос в том, как мне заставить это работать для внедрения службы?

import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.RequestHandler
import javax.enterprise.context.ApplicationScoped
import javax.inject.Inject


class HelloRequest() {
    var firstName: String? = null
    var lastName: String? = null
}

@ApplicationScoped
open class HelloGreeter() {
    open fun greet(firstName: String?, lastName: String?): String {
        return "$firstName, $lastName"
    }
}

class HelloLambda : RequestHandler<HelloRequest, String> {

    @Inject
    lateinit var greeter: HelloGreeter

    override fun handleRequest(request: HelloRequest, context: Context): String {
        return greeter.greet(request.firstName, request.lastName)
    }
}

Similar questions: "This problem results as a combination of how Kotlin handles annotations and the lack of the a @Target on the ... annotation definition. Add @field: xxx"

  • Error to inject some dependency with kotlin + quarkus
  • SmallRye Reactive Messaging's Emitter<>.send doesn't send in Kotlin via AMQP broker with Quarkus


  • person oztimpower    schedule 28.09.2019    source источник


    Ответы (1)


    Я протестировал модифицированный @field: xx со стандартной службой rest, и, квалифицировав его с помощью @field: ApplicationScoped, теперь он работает.

    Ответ на мой вопрос выше будет заключаться в том, что при использовании расширения Quarkus Amazon Lambda нет CDI среды выполнения.

    import javax.enterprise.context.ApplicationScoped
    import javax.inject.Inject
    import javax.ws.rs.GET
    import javax.ws.rs.Path
    import javax.ws.rs.PathParam
    import javax.ws.rs.Produces
    import javax.ws.rs.core.MediaType
    
    
    @ApplicationScoped
    open class GreetingService2 {
    
        fun greeting(name: String): String {
            return "hello $name"
        }
    
    }
    
    @Path("/")
    class GreetingResource {
    
        @Inject
        @field: ApplicationScoped
        lateinit var service: GreetingService2
    
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        @Path("/greeting/{name}")
        fun greeting(@PathParam("name") name: String): String {
            return service.greeting(name)
        }
    
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        fun hello(): String {
            return "hello"
        }
    }
    
    person oztimpower    schedule 10.10.2019