I'm pretty new to kotlin but done some extensive java before...
I tried to use kotlin and java in the same project but it looked very ugly and I didn't like that my classes were separated into two places. So now my project is 100% kotlin and there are problems...
I keep getting this error message:
lateinit property serviceX has not been initialized
I understand it means that service is not initialized yet, but how do i do initialized it then?
with java and spring, I did @Autowired or injected services in the constructor and was done.
what I need is for my REST to be able to use serviceX
@Component
@Path("/super")
open class SuperRest : BaseRest() {
@PUT
@Path("/test")
fun putTest(
@Context securityContext: SecurityContext,
@NotNull selected: String
) {
val user = serviceX(securityContext)
}
}
and in BaseRest I have tried to autowire serviceX first, it was:
@Autowired
protected lateinit var serviceX: ServiceX
then it was just
@Autowired
lateint var serviceX: ServiceX
then I tried other solutions I found in SO
open class BaseRest @Autowired constructor(
private val serviceX: ServiceX
) { ... }
Why is this? I can still use other @autowired services in my rest just fine. Just when I try to use them from BaseRest extension i will get
lateinit property serviceX has not been initialized
even without any lateinit property, i still somehow get this error..
private var serviceX: ServiceX = ServiceX()
it is still somehow not initialized, please help
Not sure to understand the
Just when I try to use them from BaseRest extension i will get
part, but here is the code I came across and it's working fine as expected.
open class BaseRest {
@Autowired
protected lateinit var serviceX: ServiceX
fun baseFun() {
serviceX.serviceFun(SecurityContextImpl())
}
}
@Component
open class SuperRest : BaseRest() {
fun putTest(
securityContext: SecurityContext,
@NotNull selected: String
) {
baseFun()
val user = serviceX.serviceFun(securityContext)
}
}
Bonus question : Why do you use @PUT and @Path("/test")
annotations and not @PutMapping("/test")
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.