SpringBoot之spring-boot-configuration-processor

编程知识 更新时间:2023-04-05 04:58:23

在SpringBoot的项目中,往往需要一些配置,使用idea添加配置有些都会给提示,这个是怎么实现的呢。如下图

自定义配置怎么出现提示呢?

使用spring-boot-configuration-processor实现,其作用是生产配置元数据。

1. 添加下列依赖

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

2. 在resources添加/META-INF/spring-configuration-metadata.json,也可使用additional-spring-configuration-metadata.json

json文件如下格式:

{
  "groups": [],
  "hints": [],
  "properties": [
    {
      "name": "muser.name",
      "type": "java.lang.String",
      "description": "username"
    },
    {
      "name": "muser.password",
      "type": "java.lang.String",
      "description": "password"
    },
    {
      "name": "muser.host",
      "type": "java.lang.String",
      "defaultValue": "localhost",
      "description": "hostname"
    },
    {
      "name": "muser.port",
      "type": "java.lang.Integer",
      "defaultValue": "8080",
      "description": "port"
    }
  ]
}

3. 对应的Properties:

@ConfigurationProperties(prefix = "muser")
@Component
public class UserProperties {
    private String name;
    private String password;
    private String ip;
    private int port;

   //getter setter...
}

4. 出现的效果如下:

 注:additional-spring-configuration-metadata.json中的默认值不代表Properties中已经设置,spring-boot-configuration-processor仅仅只是辅助作用

更多推荐

SpringBoot之spring-boot-configuration-processor

本文发布于:2023-04-05 04:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/3f5db341222d402c6e666d84ea415d19.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:spring   SpringBoot   boot   processor   configuration

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 44898文章数
  • 14阅读数
  • 0评论数