【发布时间】:2020-12-29 14:20:57
【问题描述】:
我无法在我的 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
附加弹簧配置-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
我错过了什么?任何和所有的帮助将不胜感激。谢谢!
【问题讨论】:
标签: spring-boot kotlin gradle intellij-idea annotation-processing