【问题标题】:How to enable Eclipse auto-completion in application.yml for custom properties declared by @ConfigurationProperties?如何在 application.yml 中为 @ConfigurationProperties 声明的自定义属性启用 Eclipse 自动完成?
【发布时间】:2019-09-03 18:43:01
【问题描述】:

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

这是课程:

@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 Boot 配置处理器的依赖项添加到 pom.xml 中,但它没有按预期工作。

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

尝试切换到 application.properties 但自动补全仍然不起作用。

【问题讨论】:

    标签: eclipse spring-boot configuration autocomplete


    【解决方案1】:

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

      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    

    【讨论】:

    • 当我遇到这个问题时,我已经在 pom.xml 中定义了 spring-boot-maven-plugin。也许是 Eclipse/SpringBootFramework 版本特定的错误,在以后的版本中修复
    【解决方案2】:

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

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

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

    【讨论】:

    • 对我不起作用,注释处理器仍然没有生成json文件:(
    • 您的 IDE 中是否安装了 Spring Tools 4 - For Spring Boot?据我记得,Application.yaml 语法高亮是包的一部分。
    • 我已经安装了但是没办法。一定有什么我错过了
    • 我刚刚注意到,自上次 IDE 更新以来,Factory Path 选项卡中的列表包含可疑的许多项目。这不像我的答案中的屏幕截图 - 现在有几十个职位。也许我的回答已经过时或者你的系统有其他问题。
    • 你在等什么 JSON 文件?您的自定义值的自动完成功能是否有效?
    猜你喜欢
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 2014-02-25
    • 1970-01-01
    相关资源
    最近更新 更多