Intellij IDEA 抱怨无法解析 spring boot 属性,但它们工作正常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48954087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intellij IDEA complains cannot resolve spring boot properties but they work fine
提问by Stoopkid
Can not resolve configuration property '...
无法解析配置属性'...
I have no problem accessing my properties through the @Value annotation or through an autowired Evironment. But all of my own defined properties get this warning in IDEA. What should I be doing to get IDEA to recognize these and not bother me?
我通过@Value 注释或自动装配的环境访问我的属性没有问题。但是我自己定义的所有属性在 IDEA 中都会收到此警告。我应该怎么做才能让 IDEA 识别这些而不打扰我?
回答by Igor Akkerman
In order for IntelliJ IDEA to know your Spring Boot properties, you can define Spring Boot configuration metadatain your project.
为了让 IntelliJ IDEA 知道您的 Spring Boot 属性,您可以在项目中定义Spring Boot 配置元数据。
Option 1:
选项1:
If you can use a @ConfigurationProperties-annotated class for your properties, you can add the Spring Boot configuration annotation processor to your classpath and IntelliJ IDEA will generate the configuration metadata for you in targetor out:
如果您可以@ConfigurationProperties为您的属性使用-annotated 类,您可以将 Spring Boot 配置注释处理器添加到您的类路径中,IntelliJ IDEA 将在target或 中为您生成配置元数据out:
Maven:
马文:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Gradle:
摇篮:
implementation 'org.springframework.boot:spring-boot-configuration-processor'
Option 2:
选项 2:
Create the configuration metadata file yourself src/main/resources/META-INF/spring-configuration-metadata.json:
自己创建配置元数据文件src/main/resources/META-INF/spring-configuration-metadata.json:
Content:
内容:
{
"properties": [
{
"name": "myapp.someprop",
"type": "java.lang.String"
},
{
"name": "myapp.someintprop",
"type": "java.lang.Integer"
}
]
}
Options 1 and 2:
选项 1 和 2:
In the IntelliJ IDEA tool window of your build system (Maven/Gradle), click the "Refresh" button.
在构建系统(Maven/Gradle)的 IntelliJ IDEA 工具窗口中,单击“刷新”按钮。
Select Build > Rebuild Projectfrom the menu.
选择Build > Rebuild Project从菜单。
If the warning still appears, you can try to restart the IDE. Select File > Invalidate Caches / Restartand click on Invalidate and Restart.
如果警告仍然出现,您可以尝试重新启动 IDE。选择File > Invalidate Caches / Restart并单击Invalidate and Restart。
回答by Manushin Igor
Please use the following for Gradle Kotlin Script for Kotlin project:
请为 Kotlin 项目的 Gradle Kotlin 脚本使用以下内容:
plugins {
kotlin("jvm")
kotlin("kapt")
}
/* ... */
dependencies {
val configurationProcessor ="org.springframework.boot:spring-boot-configuration-processor:${BuildConstants.springBootVersion}"
kapt(configurationProcessor) // for jar
kaptTest(configurationProcessor) // for jar
annotationProcessor(configurationProcessor) // for IntelliJ Idea
}
/* ... */
kapt {
annotationProcessor("org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor")
}
/* ... */
tasks {
withType<KotlinCompile> {
dependsOn(processResources)
}
}
Kotlin Kapt is needed to work with metadata and memory.
需要 Kotlin Kapt 来处理元数据和内存。
From official Spring documentation, Spring Boot Configuration Processor generates special json file with properties metadata.
根据官方 Spring 文档,Spring Boot 配置处理器生成带有属性元数据的特殊 json 文件。
Therefore, to distribute jarwith property syntax highlight you need:
因此,要分发jar属性语法高亮,您需要:
- Ask Gradle to generate this file
- Update task sequence to generate file before
jarpackaging by usingdependsOn(not sure, that my code above is the most effective solution, however problem is solved)
- 要求 Gradle 生成这个文件
- 使用更新任务序列在
jar打包前生成文件dependsOn(不确定,我上面的代码是最有效的解决方案,但问题解决了)
However IntelliJ Idea works with annotationProcessorGradle configuration (unfortunately, I don't have exact answer, why it requires exact it). Therefore you need add the same processor into the annotationProcessorconfiguration as well.
然而,IntelliJ Idea 适用于annotationProcessorGradle 配置(不幸的是,我没有确切的答案,为什么需要确切的答案)。因此,您还需要将相同的处理器添加到annotationProcessor配置中。


