如何让自己的springBoot应用自己的配置文件有提示

	在开发springboot 应用时,往往可以看到在键入配置key时发现会有提示消息或者默认配置项,springboot是如何做到的呢?
	今天我们做一个简单的演示和配置。
	**首先导入pom依赖**
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

在springboot 项目中resources 文件夹中添加META-INF 文件夹
springboot文件示例
其次:在其中文件夹中添加文件 additional-spring-configuration-metadata.json

package com.ccbobe.escoreserver;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "self")
public class HealthCheckConfig {

    private boolean check;

    private boolean ack;

    private String mode;

    /**
     * 废弃项
     */
    private String name;

    @NestedConfigurationProperty
    private  DataConfig config;

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }

    public boolean isAck() {
        return ack;
    }

    public void setAck(boolean ack) {
        this.ack = ack;
    }

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public DataConfig getConfig() {
        return config;
    }

    public void setConfig(DataConfig config) {
        this.config = config;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


配置类中记得添加注解 @Configuration 和 @ConfigurationProperties(prefix = “self”)

additional-spring-configuration-metadata.json 中添加以下内容

{
  "groups": [
    {
      "name": "self",
      "type": "com.ccbobe.escoreserver.HealthCheckConfig",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig"
    }
  ],
  "properties": [
    {
      "name": "self.check",
      "type": "java.lang.Boolean",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "description": "check status",
      "defaultValue": true
    },
    {
      "name": "self.ack",
      "type": "java.lang.Boolean",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "description": "check ack",
      "defaultValue": false
    },
    {
      "name": "self.mode",
      "type": "java.lang.String",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "description": "check mode",
      "defaultValue": "auto"
    },
    {
      "name": "self.name",
      "type": "java.lang.String",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "description": "check name",
      "defaultValue": "default",
      "deprecation": {
        "level": "warning",
        "reason": "deprecation",
        "replacement": "self.config.desc"
      }
    },
    {
      "name": "self.config.info",
      "type": "java.lang.String",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "description": "config info",
      "defaultValue": "info"
    },
    {
      "name": "self.config.level",
      "type": "java.lang.String",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "description": "config level",
      "defaultValue": "1"
    },
    {
      "name": "self.config.desc",
      "type": "java.lang.String",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "description": "config desc",
      "defaultValue": "info"
    }
  ],
  "hints": [
    {
      "name": "self.mode",
      "values": [
        {
          "value": "auto",
          "description": "auto"
        },
        {
          "value": "manual",
          "description": "Manual"
        },
        {
          "value": "disable",
          "description": "Disable"
        }
      ]
    },
    {
      "name": "self.config.info",
      "values": [
        {
          "value": "info",
          "description": "info"
        },
        {
          "value": "error",
          "description": "error"
        },
        {
          "value": "waring",
          "description": "waring"
        }
      ]
    },
    {
      "name": "self.config.level",
      "values": [
        {
          "value": "1",
          "description": "one"
        },
        {
          "value": "2",
          "description": "two"
        },
        {
          "value": "3",
          "description": "three"
        }
      ]
    }
  ]
}

关键点说明
name:配置项key名称
type:配置项类型
sourceType:配置项来源
description:配置项描述
defaultValue:配置项提示内容,定义输入时的默认值,只是提示,并不是真正的默认值,可忽略
deprecated 是否废弃 boolean 值
level 级别 error,warning
reason 废弃原因
replacement 替代属性,为properties 全名

hints 可以给属性提供可选的值,以级描述
name 属性全名,不能为空
values 可选的值
description:描述项

最后编译完成之后将会生成文件 spring-configuration-metadata.json

{
  "groups": [
    {
      "name": "self",
      "type": "com.ccbobe.escoreserver.HealthCheckConfig",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig"
    },
    {
      "name": "self.config",
      "type": "com.ccbobe.escoreserver.DataConfig",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "sourceMethod": "getConfig()"
    }
  ],
  "properties": [
    {
      "name": "self.ack",
      "type": "java.lang.Boolean",
      "description": "check ack",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "defaultValue": false
    },
    {
      "name": "self.check",
      "type": "java.lang.Boolean",
      "description": "check status",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "defaultValue": true
    },
    {
      "name": "self.config.desc",
      "type": "java.lang.String",
      "description": "config desc",
      "sourceType": "com.ccbobe.escoreserver.DataConfig",
      "defaultValue": "info"
    },
    {
      "name": "self.config.info",
      "type": "java.lang.String",
      "description": "config info",
      "sourceType": "com.ccbobe.escoreserver.DataConfig",
      "defaultValue": "info"
    },
    {
      "name": "self.config.level",
      "type": "java.lang.Integer",
      "description": "config level",
      "sourceType": "com.ccbobe.escoreserver.DataConfig",
      "defaultValue": "1"
    },
    {
      "name": "self.mode",
      "type": "java.lang.String",
      "description": "check mode",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "defaultValue": "auto"
    },
    {
      "name": "self.name",
      "type": "java.lang.String",
      "description": "check name",
      "sourceType": "com.ccbobe.escoreserver.HealthCheckConfig",
      "defaultValue": "default",
      "deprecated": true,
      "deprecation": {
        "level": "warning",
        "reason": "deprecation",
        "replacement": "self.config.desc"
      }
    }
  ],
  "hints": [
    {
      "name": "self.config.info",
      "values": [
        {
          "value": "info",
          "description": "info"
        },
        {
          "value": "error",
          "description": "error"
        },
        {
          "value": "waring",
          "description": "waring"
        }
      ]
    },
    {
      "name": "self.config.level",
      "values": [
        {
          "value": "1",
          "description": "one"
        },
        {
          "value": "2",
          "description": "two"
        },
        {
          "value": "3",
          "description": "three"
        }
      ]
    },
    {
      "name": "self.mode",
      "values": [
        {
          "value": "auto",
          "description": "auto"
        },
        {
          "value": "manual",
          "description": "Manual"
        },
        {
          "value": "disable",
          "description": "Disable"
        }
      ]
    }
  ]
}

此时打开配置文件则已经产生了配置信息提示消息

如果想在不编译的情况下有提示,则需要将编译出来的spring-configuration-metadata.json 复制到META-INF 文件夹中同样可以发生提示
希望对大家有所帮助
代码地址
参考:https://www.jianshu.com/p/497d6a231b65

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值