springspring-bootkotlinspring-propertiesconfigurationproperties

Kotlin application.yml data class looking for beans


I have a Kotlin data class that I want to read properties from application.yml file. Here is my data class:

@ConstructorBinding
@ConfigurationProperties("meanwhile.in.hell.myapp")
data class MyAppProperties(val serverId: String, val locationId: String)

I then added it to my configuration class:

@Configuration
@EnableConfigurationProperties(MyAppProperties::class)
open class MyAppConfiguration(private val properties: MyAppProperties) {

where I access the values using just properties.serverId and pass the object into the constructor of other beans being created, such as this one:

open class MyAppClient(
    private val webClient: WebClient,
    private val properties: MyAppProperties
) : IMyAppClient {

However, when I start up my application I get an error that instead of trying to load the properties from application.yml, it is trying to find beans for the constructor params:

Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'meanwhile.in.hell.myapp-meanwhile.in.hell.myapp.MyAppProperties': 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: {}

Parameter 0 of constructor in meanwhile.in.hell.myapp.MyAppProperties required a bean of type 'java.lang.String' that could not be found.

How do I stop my app thinking that these params are Autowired? I know that in Kotlin, this is how a constructor Autowired bean looks like (ie, not requiring the annotation), but all example I have seen online on how to read application.yml properties looks the same as my data class.

Spring-Boot v2.3.0.RELEASE Kotlin v1.3.72


Solution

  • Turns out I was missing the dependency

    org.jetbrains.kotlin:kotlin-reflect

    Docs: https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/htmlsingle/#boot-features-kotlin-requirements

    Issue: https://github.com/spring-projects/spring-boot/issues/19582