@ConfigurationProperties is not working in Spring Boot 3.0.1

1.1k Views Asked by At

I faced the issue after upgrade SpringBoot from 2.x to 3.0.1 In kotlin, @ConfigurationProperties deosn't generate 'peroperteis' field in some conditions.

1. Code: 'var' with default value

@ConfigurationProperties(prefix = "cnr.app")
data class AppProperties(
    var name: String = "cma",
    var version: String = "0.0.1"
)

spring-configuration-metadata.json

{
  "groups": [
    {
      "name": "cnr.app",
      "type": "studio.m2s.cnr.cma.AppProperties",
      "sourceType": "studio.m2s.cnr.cma.AppProperties"
    }
  ],
  "properties": [
    {
      "name": "cnr.app.name",
      "type": "java.lang.String",
      "sourceType": "studio.m2s.cnr.cma.AppProperties"
    },
    {
      "name": "cnr.app.version",
      "type": "java.lang.String",
      "sourceType": "studio.m2s.cnr.cma.AppProperties"
    }
  ],
  "hints": []
}

result: Good

2. Code: 'val' with default value

@ConfigurationProperties(prefix = "cnr.app")
data class AppProperties(
    val name: String = "cma",
    val version: String = "0.0.1"
)

spring-configuration-metadata.json

{
  "groups": [
    {
      "name": "cnr.app",
      "type": "studio.m2s.cnr.cma.AppProperties",
      "sourceType": "studio.m2s.cnr.cma.AppProperties"
    }
  ],
  "properties": [],
  "hints": []
}

result: Bad.

In Spring Boot 3.0.1

  • If I declare fields with 'var', it works good.
  • If I declare fields with 'val', it works bad.
  • If I declare fields with 'var' without default value, it works good.
  • If I declare fields with 'val' without default value, it works good.

In Spring Boot 2.7.x

  • If I declare fields with 'var', it works good.
  • If I declare fields with 'val', it works good.
  • If I declare fields with 'var' without default value, it works good.
  • If I declare fields with 'val' without default value, it works good.

gradle.kts

configurations {
    compileOnly {
        extendsFrom(configurations.annotationProcessor.get())
    }
}

kapt {
    annotationProcessor("org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor")
}

tasks {
    withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
        dependsOn(processResources)
    }
}


dependencies {
    kapt("org.springframework.boot:spring-boot-configuration-processor")
    annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
}

I expect, it works with 'val' with default value. In Spring Boot 3.0.1

  • If I declare fields with 'var', it works good.
  • If I declare fields with 'val', it works good.
  • If I declare fields with 'var' without default value, it works good.
  • If I declare fields with 'val' without default value, it works good.

like spring boot 2.7.x

1

There are 1 best solutions below

3
On

In Spring Boot 3.0.1

If I declare fields with 'var', it works good (with initial value I assume).

If I declare fields with 'val', it works bad (with initial value I assume).

If I declare fields with 'var' without default value, it works good.

If I declare fields with 'val' without default value, it works good.

This seems more like an issue of the past if it happened and not a current issue of spring-boot 3. Probably not related with spring boot but more with kotlin and jdk.

val in Kotlin is like final in java, meaning it's value could not change once assigned. So it is logical that spring can't reassign another value in case your initial value for that field is already set and the field is declared as final with val.