Intellij Idea KotlinSping Boot Annotation处理“无法解析配置处理”

8tntrjer  于 2023-05-12  发布在  Kotlin
关注(0)|答案(2)|浏览(104)

我无法在KotlinSping Boot 应用程序中正确注入@Value应用程序属性。在我的application.yml文件中定义的属性,随后在additional-spring-configuration-metadata.json文件中引用(在resources -> 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
我错过了什么?任何和所有的帮助将不胜感激。谢谢!

yvt65v4c

yvt65v4c1#

如果您的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
wfveoks0

wfveoks02#

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

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

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

相关问题