I've implemented several projects with spring boot and java. Now I'm evaluating, if I could do it with kotlin.
I am struggling with @ConfigurationProperties and data classes.
Simple example:
api:
clientId: client123
url: https://api.url.com
key: api-access-key
Data class:
@ConfigurationProperties("api")
@ConstructorBinding
data class ApiConfiguration(
val clientId: String,
val url: String,
val key: String
)
Starter class:
@EnableConfigurationProperties(ApiConfiguration::class)
@SpringBootApplication
class SpringdemoApplication
fun main(args: Array<String>) {
runApplication<SpringdemoApplication>(*args)
}
If I'm running a test, I get the following message:
s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'apiConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
...
Description:
ApiConfiguration is annotated with @ConstructorBinding but it is defined as a regular bean which caused dependency injection to fail.
Action:
Update your configuration so that ApiConfiguration is defined via @ConfigurationPropertiesScan or @EnableConfigurationProperties.
The following post doesn't help me: Kotlin & Spring Boot @ConfigurationProperties
Environment:
- spring boot 2.7.8
- kotlin 1.6.21
Can someone help me to understand and solve the problem?
@ConfigurationPropertiesScanon my starter would be enough to initializeApiConfigurationcorrectly. Am I wrong?@Configurationclass ?@Configurationclasses ... @amanin I've try to use@EnableConfigurationProperties, but it doesn't change anything.