0

I try to implement my first spring boot application using Kotlin. spring-boot version is 3.1.0

I have a configuration class like this(based on https://stackoverflow.com/a/74445643/2674303):

@Configuration
@ConfigurationProperties(prefix = "my.prefix")
data class MyProperties(
    var username: String = "",
    var privateKeyPath: String = "",

This works fine but I don't want to see default values here because they are useless and in java you don't have to have them.

I've found following post: Kotlin & Spring Boot @ConfigurationProperties

And started to apply solutions from there:

1.

import org.springframework.boot.context.properties.bind.ConstructorBinding


@Configuration
@ConfigurationProperties(prefix = "my.prefix")
@ConstructorBinding
data class MyProperties(
    var username: String = "",
    var privateKeyPath: String = "",

I get compilation error:

This annotation is not applicable to target 'class'
@Configuration
@ConfigurationProperties(prefix = "my.prefix")
data class MyProperties {
    lateinit varusername: String = "",
    lateinit var privateKeyPath: String = "",

I receive an error which points to the first fileld:

Property getter or setter expected

Have I missed something ?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • I get the error during startup: `Parameter 0 of constructor in *.*.MyProperties required a bean of type 'java.lang.String' that could not be found.` – gstackoverflow Jul 06 '23 at 10:00
  • 1
    If you don't want to provide default values, Kotlin requires that the properties are either immutable (`val`) or nullable. You should not use `@Autowired` as you want properties to be bound, not beans to be injected. – Andy Wilkinson Jul 06 '23 at 10:50
  • @Andy Wilkinson Itried 2 options: 1. `val username: String?,` => `Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate .... No default constructor found` – gstackoverflow Jul 06 '23 at 12:41
  • 2. `var username: String?,` =>`Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate...No default constructor found` – gstackoverflow Jul 06 '23 at 12:43
  • @Andy Wilkinson I would appreciate if you could provide the whole example – gstackoverflow Jul 06 '23 at 12:44
  • You should not annotate your class with both `@Configuration` and `@ConfigurationProperties`. Your class is either a configuration properties pojo, or a spring configuration class. Mixing both might lead to unexpected results. If you need your properties from a configuration class, just inject the property pojo in the configuration class. You can check this kotlin configuration properties object in [an example project of mine](https://github.com/alexismanin/get-started-spring-boot/blob/main/src/main/kotlin/fr/amanin/demos/hellospringboot/HelloSpringBootApplication.kt#L95) – amanin Jul 06 '23 at 14:41
  • @amanin when I put annotation `@ConstructorBinding` over MyProperties I receive error: "This annotation is not applicable to target 'class'" – gstackoverflow Jul 12 '23 at 07:44
  • @amanin Without ConstructorBinding your solution is working, Would ypu like to add answer ? I will accept it – gstackoverflow Jul 12 '23 at 18:20

1 Answers1

0

This works:

@ConfigurationProperties(prefix = "my.prefix")
data class MyProperties {
    lateinit varusername: String = "",
    lateinit var privateKeyPath: String = "",
 ...

@Configuration
@EnableConfigurationProperties(MyProperties ::class)
class LdapConnectionPoolConfig(
    private val myProperties : MyProperties 
) {
  ....
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710