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

Ale*_*kin 3 eclipse configuration autocomplete spring-boot

我为我的 spring-boot 项目创建了一个带有配置属性的简单类。一切都像一个魅力(spring-boot 捕获选项),除了 Eclipse 不将新属性识别为application.yml中的有效选项并将其突出显示为未知这一事实之外。

这是课程:

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

自动补全不适用于以下属性:

自动补全不适用于这些属性

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

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

尝试切换到application.properties但自动完成功能仍然不起作用。

Ale*_*kin 9

Spring Boot 配置处理器在编译期间充当注释处理器。

需要为 Eclipse 项目启用注释处理并注册处理器:

  • 转到项目/属性菜单
  • 打开Java 编译器/注释处理。启用项目特定设置并选中“启用注释处理”
  • 打开Java编译器/注释处理/工厂路径。选中“启用项目特定设置”
  • 点击“添加变量”按钮,选择“M2_REPO”,点击“扩展”,在Maven仓库中找到org/springframework/boot/spring-boot-configuration-processor/xxxRELEASE/spring-boot-configuration-processor-xxxRELEASE.jar ,其中 xxx 是 Spring Boot 的版本。
  • 应用更改
  • 重新编译项目,或者只需触摸并保存具有配置属性的类即可触发部分重新编译。

在此输入图像描述