如何在 application.yml 中为 @ConfigurationProperties 声明的自定义属性启用 Eclipse 自动完成?

How to enable Eclipse auto-completion in application.yml for custom properties declared by @ConfigurationProperties?

我为我的 spring-boot 项目制作了一个带有配置属性的简单 class。除了 Eclipse 在 application.yml 中不将新属性识别为有效选项并将它们突出显示为未知。

这里是 class:

@Component
@ConfigurationProperties(prefix="server")
public class ServerProperties {
    private Integer delay;
    private DataAdapter dataAdapter = new DataAdapter();
    // setters and getters were cut out for the sake of readability 

    public static class DataAdapter {
        private String baseUrl;
        private String targetCode;
        // setters and getters 
    }
}

自动完成不适用于这些属性:

我按照 Spring.io reference 中的建议将 Spring 引导配置处理器的依赖项添加到 pom.xml,但它没有按预期工作.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

已尝试切换到 application.properties,但自动完成功能仍然无效。

Spring 引导配置处理器在编译时用作注释处理器。

需要为Eclipse项目开启注解处理并注册处理器:

  • 转到项目/属性菜单
  • 打开Java编译器/注解处理。启用项目特定设置并选中 "Enable annotation processing"
  • 打开Java编译器/注解处理/工厂路径。检查 "Enable project specific settings"
  • 点击"Add variable"按钮,select"M2_REPO",点击"Extend"找到org/springframework/boot/spring-boot-configuration-processor/x.x.x.RELEASE/spring-boot-configuration-processor-x.x.x.RELEASE.jar 在 Maven 存储库中,其中 x.x.x 是您的 Spring开机
  • 应用更改
  • 重新编译项目,或者只需触摸并保存带有配置属性的 class 即可触发部分重新编译。

我遇到了同样的问题并通过将其添加到我的 pom.xml:

来修复它
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>