Q: Fail to initialize a property with default value of @Value annotation in Kotlin I have a property that holds Redis port in my Redis config class.
@Value("\${redis.port:63799}")
lateinit var port: Integer

init {
        this.redisServer = RedisServer(port.toInt())
    }

When I run my code I get this error:
kotlin.UninitializedPropertyAccessException: lateinit property port has not been initialized


A: @Value annotation is processed after the object is constructed.
The init block is part of object construction (it's common for all your constructors).
In other words, you're trying to reference the @Value injected property too early.
One way to work around it is to delay RedisServer construction in some way, i.e.:


*

*Use lazyinit for the RedisServer.

*Use @PostConstruct on a method to initialise the RedisServer.

