首页 > 编程 > Java > 正文

Spring Boot自定义配置实现IDE自动提示功能

2019-11-26 08:42:18
字体:
来源:转载
供稿:网友

一、背景

官方提供的spring boot starter的配置项,我们用IDE配置的时候一般都有自动提示的,如下图所示

而我们自己自定义的配置却没有,对开发非常不友好容易打错配置, 那这个是怎样实现的呢?

二、提示原理

IDE是通过读取配置信息的元数据而实现自动提示的,而元数据在目录 META-INF 中的 spring-configuration-metadata.json 或者 additional-spring-configuration-metadata.json

三、实现自动提示

以我这个自己开发的starter中的自定义配置文件为例,如果自己手动创建这些元数据的话工作量比较大,使用 IDEA 的话有自动生成功能

3.1. 引入依赖spring-boot-configuration-processor

zlt-swagger2-spring-boot-starter 工程中添加以下jar包

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

3.2. 修改IDEA配置

搜索 Annotation Processor 并设置 Enable annotation processing


3.3. 重新编译项目

项目在重新编译后就会自动生成 spring-configuration-metadata.json 文件

四、测试

自定义的swagger配置已经能自动提示了

参考资料

https://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html

总结

以上所述是小编给大家介绍的Spring Boot自定义配置实现IDE自动提示功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表