Kotlin Spring Boot批注处理“无法解析配置处理”

问题描述

我无法在Kotlin Spring Boot应用程序中正确插入@Value应用程序属性。我在application.yml文件中定义并随后在additional-spring-configuration-Metadata.json文件中引用的属性(在资源-> meta-inf下)没有正确地添加到bean表达式上下文中。使用IntelliJ version 2020.2.1,当我将鼠标悬停在属性上时,看到一个Cannot resolve configuration property错误。尝试运行应用程序(将构造属性值构造注入到类中)会导致Unsatisfied dependency expressed through constructor parameter错误

build.gradle.kts


plugins {
    id("org.springframework.boot") version "2.3.3.RELEASE"
    id("io.spring.dependency-management") version "1.0.10.RELEASE"
    kotlin("jvm") version "1.3.72"
    kotlin("plugin.spring") version "1.3.72"
}

group = "com.myProject"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

extra["springCloudVersion"] = "Hoxton.SR8"

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation("org.springframework.boot:spring-boot-configuration-processor:2.3.3.RELEASE")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage",module = "junit-vintage-engine")
    }
    testImplementation("io.projectreactor:reactor-test")
}

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("com.google.cloud.tools:appengine-gradle-plugin:2.2.0")
    }
}

apply(plugin = "com.google.cloud.tools.appengine")

configure<com.google.cloud.tools.gradle.appengine.appyaml.AppEngineAppYamlExtension> {
    deploy {
        projectId = "my-cloud-project"
        version = "GCLOUD_CONfig"
    }
}

dependencyManagement {
    imports {
        mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinoptions {
        freeCompilerArgs = listof("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

错误消息 Spring error modal

additional-spring-configuration-Metadata.json

{
  "properties": [
    {
      "name": "otherApi.baseUrl","type": "java.lang.String","description": "Description for otherApi.baseUrl."
    }
  ]
}

添加了注释处理依赖性,无效的缓存并重新启动,并与Kotlin特定的注释处理器(kapt)一起玩。我也按照这里的指示进行:https://www.jetbrains.com/help/idea/annotation-processors-support.html

我想念什么?任何和所有帮助将不胜感激。谢谢!

解决方法

您需要将以下内容声明为annoatationProcessor

implementation("org.springframework.boot:spring-boot-configuration-processor:2.3.3.RELEASE")

annotationProcessor("org.springframework.boot:spring-boot-configuration-processor:2.3.3.RELEASE")
,

如果您的 application.yml(或 application.properties)如下所示:

spring:
    datasource:
        username: postgres
        password: postgres
        url: jdbc:postgresql://localhost:5433/company
        driver-class-name: org.postgresql.Driver

然后尝试将每个属性重写为每个属性的全名格式:

spring.datasource.username: postgres
spring.datasource.password: postgres
spring.datasource.url: jdbc:postgresql://localhost:5433/company
spring.datasource.driver-class-name: org.postgresql.Driver