@ConfigurationProperties在类路径中找不到Spring Boot配置注释处理器

Fee*_*eco 46 java spring intellij-idea spring-boot

我尝试在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)
Run Code Online (Sandbox Code Playgroud)

之后,我尝试重建项目并在设置中启用注释处理器,但通知尚未消失.完成也不起作用(我创建了一个字符串my).

小智 24

我有同样的问题.我使用idea 2017.2和gradle 4.1,有些博客说你应该添加:

dependencies {
    optional "org.springframework.boot:spring-boot-configuration-processor"
}
Run Code Online (Sandbox Code Playgroud)

但我把它改成了这个:

dependencies {
    compile "org.springframework.boot:spring-boot-configuration-processor"
}
Run Code Online (Sandbox Code Playgroud)

警告消失了.


naX*_*aXa 12

根据Spring Boot docs,自Gradle 4.6起的正确配置是

dependencies {
    annotationProcessor group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是IntelliJ IDEA尚不支持annotationProcessor范围。如果您想提请注意此问题,请升级IDEA-187868


Fee*_*eco 10

我忘了添加propdeps-plugin.但是,我记得即使在2016.3上使用插件它也不适合我.所以正如@CrazyCoder所提到的,尝试降级Gradle或下载新的2017.1版本(详情).
您也可以Re-run Spring Boot Configuration Annotation Processor to update generated metadata在解决此问题时收到.为此,单击Refresh all Gradle projects(在Gradle侧面菜单中).

  • 仍然不适合我.我添加了`propdeps-plugin`,`optional`行和`compileJava.dependsOn(processResources)`,获得了IntelliJ IDEA 2017.3.尝试在Gradle中运行`clean`任务,然后`Refresh all`和`Build`,仍然没有.任何的想法? (2认同)

Ole*_*kov 10

在 maven 项目中帮助添加依赖 spring-boot-configuration-processor 并使用 @EnableConfigurationProperties(AppProperties.class) 标记主类。

也许有人会帮忙。


ana*_*bek 9

对于那些使用 maven 的人,Intellij 仍然对添加依赖不满意。似乎annotationProcessorPaths通过添加maven-compiler-plugin终于驯服了野兽。

确保version匹配您的 spring 依赖项。我怀疑它已经存在于您的有效 POM 中。

原因:我使用了一个自定义的 parent-pom,其中设置了一个 mapstruct 注释处理器annotationProcessorPaths,这实际上触发了 IntelliJ 要求手动指定所有其他注释处理器。

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <annotationProcessorPaths>
        <path>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-configuration-processor</artifactId>
          <version>2.0.4.RELEASE</version>
        </path>
      </annotationProcessorPaths>
    </configuration>
  </plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

  • 请不要在 Maven 中使用硬编码版本。继承,所以升级SpringBoot时只需要更改一次: &lt;version&gt;${project.parent.version}&lt;/version&gt; (6认同)
  • 显然,mapstruct 要求我们在“annotationProcessorPaths”中特别提及mapstruct,当我们开始手动指定它们时,我们还必须手动添加 spring-boot-configuration-processor、lombok 等。至少这就是intellij想要的 (4认同)
  • 我的父项目不是 spring boot 父 POM。所以这有点YMMV。 (2认同)

naX*_*aXa 7

对于 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")
}
Run Code Online (Sandbox Code Playgroud)


AR1*_*AR1 6

在 IntelliJ 2018.3 版本中,我通过以下方式解决了这个问题(根据本文档):

对于 Gradle 4.5 及更早版本,应在 compileOnly 配置中声明依赖项,如以下示例所示:

dependencies {
  compileOnly "org.springframework.boot:spring-boot-configuration-processor"
}
Run Code Online (Sandbox Code Playgroud)

对于 Gradle 4.6 及更高版本,应在 annotationProcessor 配置中声明依赖项,如以下示例所示:

dependencies {
  annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}
Run Code Online (Sandbox Code Playgroud)


Jef*_*919 6

对于使用 Maven 或 Gradle 的用户,只需添加对spring-boot-configuration-processor的依赖即可

马文:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
Run Code Online (Sandbox Code Playgroud)

Gradle 4.5 及更早版本

dependencies {
     compileOnly "org.springframework.boot:spring-boot-configuration-processor"
}
Run Code Online (Sandbox Code Playgroud)

Gradle 4.6 及更高版本

dependencies {
      annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息: “使用注释处理器生成您自己的元数据” https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/appendix-configuration-metadata.html#configuration -元数据注释处理器


小智 5

我发生这种情况的原因有两个:

  1. 仔细检查是否在IDEA中选择(启用)您的设置:“首选项”->“注释处理器”->“启用注释处理”。
  2. 更新您的Idea之后,请检查您的插件并进行更新。插件可能与您的新IDEA版本不兼容,因此只需单击以更新它们。

  • 在我的想法中,(1) 是:文件| 设置 | 构建、执行、部署 | 编译器| 注释处理器 (9认同)