Issue
I have a data class for storing properties
@Component
@ConfigurationProperties(prefix = "cache")
data class CacheProperties(
var enable: Boolean = false,
var CacheName: String = "",
)
And I have an object where I need to use these properties. The Kotlin object doesn't support the @Autowired annotation. How can I initialize the properties in this case?
object MdsCacheValidationUtil {
//need to inject cache properties here
private val log: Logger = LoggerFactory.getLogger(this::class.java)
fun validateLastUpdateTime(lastUpdateTime: Instant?) {
println(cache.CacheName)
//.....
}
}
Solution
You should turn MdsCacheValidationUtil into a Spring Managed Bean using @Component annotation as follows:
@Component
class MdsCacheValidationUtil(cacheProperties: CacheProperties) {
private val log: Logger = LoggerFactory.getLogger(this::class.java)
fun validateLastUpdateTime(lastUpdateTime: Instant?) {
println(cacheProperties.CacheName)
//.....
}
}
Additionally, I believe you have a typo in CacheProperties. I guess you wanted to write cacheName instead of CacheName. Mind the lowercase "c".
Answered By - João Dias
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.