SpringBoot自定义配置自动提示

SpringBoot项目配置文件中如果使用自定义配置时IDE工具时不会自动提示的,如果想实现自动提示可按如下操作

编写自定义配置类

使用注解@ConfigurationProperties并指定前缀

@Component
@ConfigurationProperties(prefix = "zg.river")
@Data
public class RiverGlobalProperties {

    private String notAllowRefreshIndex;

    private String traceInterTime;

    private String patrolMaxTime;
}

编写对应的配置文件

zg:
  river:
    trace-inter-time: 5
    patrol-max-time: 10

添加注解处理器

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

修改IDEA配置

Settings --> Annotation Processor --> 勾选 Enable annotation processing

编译生成提示文件

  • 重新编译代码
  • 生成的文件如下classes/META-INF/spring-configuration-metadata.json


{
  "hints": [],
  "groups": [
    {
      "sourceType": "com.zg.river.config.properties.RiverGlobalProperties",
      "name": "zg.river",
      "type": "com.zg.river.config.properties.RiverGlobalProperties"
    }
  ],
  "properties": [
    {
      "sourceType": "com.zg.river.config.properties.RiverGlobalProperties",
      "name": "zg.river.not-allow-refresh-index",
      "description": "notAllowRefreshIndex",
      "type": "java.lang.String"
    },
    {
      "sourceType": "com.zg.river.config.properties.RiverGlobalProperties",
      "name": "zg.river.patrol-max-time",
      "description": "patrolMaxTime",
      "type": "java.lang.String"
    },
    {
      "sourceType": "com.zg.river.config.properties.RiverGlobalProperties",
      "name": "zg.river.trace-inter-time",
      "description": "traceInterTime",
      "type": "java.lang.String"
    }
  ]
}
  • 之后发现自定义的配置可以自动提示,并且可以进行跳转了


参考资料:

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容