0

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?

6
  • can you show us your configuration ? You should not use @Bean or Bean dsl to register ConfigurationProperties that use ConstructorBinding. You should use EnableConfigurationProperties or configuration property scanning. Commented Feb 15, 2023 at 10:19
  • @JEY I've added my starter class. I thought using @ConfigurationPropertiesScan on my starter would be enough to initialize ApiConfiguration correctly. Am I wrong? Commented Feb 15, 2023 at 10:49
  • I've got an exemple of a mini spring-boot app in kotlin with configuration properties as immutable data class. Maybe you can check configuration differences from your build and code ? -> github.com/alexismanin/get-started-spring-boot/blob/main/src/… Commented Feb 15, 2023 at 11:31
  • @ThiloSchwarz yes it should. You don't have any other @Configuration class ? Commented Feb 15, 2023 at 13:06
  • @JEY Later I'll have a lot of @Configuration classes ... @amanin I've try to use @EnableConfigurationProperties, but it doesn't change anything. Commented Feb 15, 2023 at 19:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.