I'm trying to generate spring-configuration-metadata.json file for my Spring Boot based project. If I use Java @ConfigurationProperties class it is generated correctly and automatically:

@ConfigurationProperties("myprops")
public class MyProps {

    private String hello;

    public String getHello() {
        return hello;
    }

    public void setHello(String hello) {
        this.hello = hello;
    }
}

但如果我使用Kotlin类,则不会生成spring-configuration-metadata.json文件(我try 了gradle build和Idea Rebuild Project).

@ConfigurationProperties("myprops")
class MyProps {
    var hello: String? = null
}

AFAIK Kotlin使用构造函数、getter和setter生成同一个类,应该充当常规的Java bean.

Any ideas why spring-boot-configuration-processor doesn't work with Kotlin classes?

推荐答案

Thank you for pointing me in the right direction. So the solution is to add

dependencies {
    ...
    kapt "org.springframework.boot:spring-boot-configuration-processor"
    optional "org.springframework.boot:spring-boot-configuration-processor"
    ...
}

to build.gradle file, run gradle compileJava in command line and turn on annotation processing in IntelliJ Idea settings Build, Execution, Deployment -> Compiler -> Annotation processor -> Enable anotation processing. The rest of configuration remains the same

还要注意的是,如果没有这一行

optional "org.springframework.boot:spring-boot-configuration-processor"

IntelliJ Idea will complain whith

Cannot resolve configuration property

message in your application.properties or application.yml

Kotlin相关问答推荐

如何确保Kotlin子类已完成初始化?

如何在不基于数据 map 的情况下将图例添加到lets plot kotlin

用于将 0.5 变为 0 的 round() 函数的模拟

JetPack Compose:添加点击持续时间

测试协程和线程之间的差异,我在kotlin中使用线程时无法得到OOM错误

匹配在单词边界上包含特殊字符的变量字符串的正则表达式

Kotlin:不允许在辅助构造函数参数上使用val

Dagger 2 ContributesAndroidInjector 为模块提供活动

使用 Hilt 注入 CoroutineWorker

模拟异常 - 没有找到答案

如何处理 Kotlin 中的异常?

哪里可以找到aapt2日志(log)?

更新到版本4.10.1/4.10.2后 Gradle 同步失败

从 Spring WebFlux 返回 Flux 返回一个字符串而不是 JSON 中的字符串数组

在Kotlin中传递并使用函数作为构造函数参数

如何获取Kotlin中变量的名称?

Kotlin reflect proguard SmallSortedMap

无法解决:androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1

Gradle:无法连接到 Windows 上的 Kotlin 守护程序

Kotlin:测试中的 java.lang.NoSuchMethodError