Java 在类路径中找不到@ConfigurationProperties Spring Boot 配置注释处理器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42839126/
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
@ConfigurationProperties Spring Boot Configuration Annotation Processor not found in classpath
提问by Feeco
I try to make completion for custom properties in Spring Boot.
I tried to create a simple project via IntelliJ IDEA 2016.3:
1. Created a new Gradle project with Spring Boot Initializer(I haven't checked nothing at all).
2. Created a new class Properties
.
3. When I annotated it with @ConfigurationProperties
, the next notification has appeared:
The documentationsaid that I should add the following to my project:
我尝试在Spring Boot 中完成自定义属性。
我尝试通过IntelliJ IDEA 2016.3创建一个简单的项目:
1. 使用Spring Boot Initializer创建了一个新的 Gradle 项目(我根本没有检查任何东西)。
2. 创建了一个新类Properties
。
3.当我用 注释它时@ConfigurationProperties
,出现了下一个通知:
文档说我应该将以下内容添加到我的项目中:
dependencies {
optional "org.springframework.boot:spring-boot-configuration-processor"
}
compileJava.dependsOn(processResources)
After that I tried to rebuild project and enable annotation processors in settings but the notification hasn't gone. Completion doesn't work too (I created a string my
).
之后,我尝试重建项目并在设置中启用注释处理器,但通知没有消失。完成也不起作用(我创建了一个字符串my
)。
采纳答案by Icex
I had the same problem. I use idea 2017.2 and gradle 4.1, and some blog said you should add:
我有同样的问题。我使用的是idea 2017.2和gradle 4.1,有些博客说你应该添加:
dependencies {
optional "org.springframework.boot:spring-boot-configuration-processor"
}
But I changed it to this:
但是我把它改成了这样:
dependencies {
compile "org.springframework.boot:spring-boot-configuration-processor"
}
And the warning is gone.
并且警告消失了。
回答by Feeco
I forgot to add propdeps-plugin. However, I remember that it didn't work for me even with the plugin on 2016.3, So as @CrazyCoder mentioned, try to downgrade Gradle or download the new 2017.1 version (details).
Also you may receive Re-run Spring Boot Configuration Annotation Processor to update generated metadata
when you will solve this issue. For this, click Refresh all Gradle projects
(in Gradle side menu).
我忘了添加propdeps-plugin。但是,我记得即使使用 2016.3 上的插件它也对我不起作用,所以正如@CrazyCoder 提到的,尝试降级 Gradle 或下载新的 2017.1 版本(详细信息)。
您也可能会收到Re-run Spring Boot Configuration Annotation Processor to update generated metadata
何时解决此问题。为此,单击Refresh all Gradle projects
(在 Gradle 侧边菜单中)。
回答by Dirk Hoffmann
following works for me:
以下对我有用:
buildscript {
repositories {
jcenter()
maven { url 'https://repo.jenkins-ci.org/public/' }
maven { url 'http://repo.spring.io/plugins-release' }
}
dependencies {
classpath "io.spring.gradle:propdeps-plugin:0.0.9.RELEASE"
}
}
...
apply plugin: 'propdeps'
apply plugin: 'propdeps-eclipse'
apply plugin: 'propdeps-idea'
...
dependencyManagement {
imports {
mavenBom 'org.springframework.boot:spring-boot-starter-parent:2.0.0.RELEASE'
}
}
...
dependencies {
compile "org.springframework.boot:spring-boot-starter"
compile "org.springframework.boot:spring-boot-starter-actuator"
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" // for @ConfigurationProperties, make sure compileJava.dependsOn(processResources)
...
}
compileJava.dependsOn(processResources)
回答by vargapeti
I had the same problem with IntelliJ version 2018.1.2. I also had to define the actual version of spring-boot-configuration-processor in order to get it worked:
我在 IntelliJ 版本 2018.1.2 上遇到了同样的问题。我还必须定义 spring-boot-configuration-processor 的实际版本才能使其正常工作:
compile('org.springframework.boot:spring-boot-configuration-processor:2.0.1.RELEASE')
回答by naXa
According to the Spring Boot docs, the correct configuration since Gradle 4.6 is
根据Spring Boot 文档,自 Gradle 4.6 以来的正确配置是
dependencies {
annotationProcessor group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
// ...
}
IntelliJ IDEA supports annotationProcessor
scope since build 193.3382 (2019.3). Don't forget to enable annotation processingin IntelliJ IDEA settings.
annotationProcessor
自构建 193.3382 (2019.3) 以来,IntelliJ IDEA 支持作用域。不要忘记在 IntelliJ IDEA 设置中启用注释处理。
回答by AR1
In version 2018.3 of IntelliJ, I solved this problem (as per this documentation) in the following way:
在 IntelliJ 2018.3 版本中,我通过以下方式解决了这个问题(根据本文档):
With Gradle 4.5 and earlier, the dependency should be declared in the compileOnly configuration, as shown in the following example:
dependencies { compileOnly "org.springframework.boot:spring-boot-configuration-processor" }
With Gradle 4.6 and later, the dependency should be declared in the annotationProcessor configuration, as shown in the following example:
dependencies { annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" }
对于 Gradle 4.5 及更早版本,应在 compileOnly 配置中声明依赖项,如以下示例所示:
dependencies { compileOnly "org.springframework.boot:spring-boot-configuration-processor" }
对于 Gradle 4.6 及更高版本,应在 annotationProcessor 配置中声明依赖项,如以下示例所示:
dependencies { annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" }
回答by Victor Schurenko
It happens to me for two reasons in IDEA:
在 IDEA 中发生这种情况有两个原因:
- Double check if your setting is picked (enabled) in IDEA: Preferences->Annotation Processors->Enable annotation processing.
- After update your Idea, check your plugins and update them. It happens that plugins become incompatible with your new IDEA version, so just click to update them.
- 仔细检查您的设置是否在 IDEA 中被选择(启用):首选项->注释处理器->启用注释处理。
- 更新您的想法后,检查您的插件并更新它们。碰巧插件与您的新 IDEA 版本不兼容,因此只需单击即可更新它们。
回答by Oleg Pavlyukov
In maven project helps adding dependency spring-boot-configuration-processor and marking main class with @EnableConfigurationProperties(AppProperties.class).
在 maven 项目中帮助添加依赖 spring-boot-configuration-processor 并使用 @EnableConfigurationProperties(AppProperties.class) 标记主类。
Maybe somebody helps.
也许有人会帮忙。
回答by naXa
For Kotlin projects, the working configuration since Gradle 4.6 is using annotation processor
对于 Kotlin 项目,从 Gradle 4.6 开始的工作配置是使用注解处理器
apply plugin: "kotlin-kapt"
dependencies {
kapt("org.springframework.boot:spring-boot-configuration-processor:$springBootVersion")
compileOnly("org.springframework.boot:spring-boot-configuration-processor:$springBootVersion")
}
回答by naXa
According to the Spring Boot docs, the correct configuration for Gradle 4.5 and earlier is
根据Spring Boot 文档,Gradle 4.5 及更早版本的正确配置是
dependencies {
compileOnly group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
// ...
}