“ @ ApplicationScope”为服务定义的@Inject注释无法在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)
}
}
类似的问题: “此问题是由于Kotlin处理注释的方式以及...注释定义中缺少@Target的组合。添加@field:xxx”
答案 0 :(得分:0)
我使用标准的REST服务测试了修改后的@field:xx,并通过@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"
}
}