Kotlin Spring Boot 注解处理“无法解析配置处理”
Posted
技术标签:
【中文标题】Kotlin Spring Boot 注解处理“无法解析配置处理”【英文标题】:Kotlin Spring Boot Annotation Processing "Cannot Resolve Configuration Processing" 【发布时间】: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
我错过了什么?任何和所有的帮助将不胜感激。谢谢!
【问题讨论】:
【参考方案1】:您需要将以下内容声明为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")
【讨论】:
我试过了,还是不行。但这是一个很好的建议,所以谢谢!我确实认为这对我的 Kotlin 项目来说确实非常挑剔。我会继续玩【参考方案2】:如果您的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
【讨论】:
以上是关于Kotlin Spring Boot 注解处理“无法解析配置处理”的主要内容,如果未能解决你的问题,请参考以下文章
spring boot+mybatis注解使用方式(无xml配置)设置自动驼峰明明转换(),IDEA中xxDao报错could not autowire的解决方法
Spring Boot源码分析@EnableAutoConfiguration注解@AutoConfigurationImportSelector注解的处理