如何让自己的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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,出现`java.lang.NullPointerException`异常可能是因为在使用其他文件的service时,没有正确注入或初始化service对象。以下是可能导致该异常的几种常见情况和解决方法: 1. 检查是否正确注入service对象:确保在使用其他文件的service之前,已经正确注入了该service对象。可以使用`@Autowired`注解或者构造函数注入的方式来注入service对象。 2. 检查service对象是否被正确初始化:如果service对象没有被正确初始化,就会导致NullPointerException异常。可以通过在service类上添加`@Service`注解来确保该类被正确初始化。 3. 检查是否正确配置了依赖注入:如果使用了依赖注入框架(如Spring),请确保已经正确配置了依赖注入。可以检查是否在配置文件中正确配置了相关的bean。 4. 检查是否正确引入了相关的依赖:如果使用了其他的库或框架,需要确保已经正确引入了相关的依赖。可以检查项目的依赖配置文件(如pom.xml或build.gradle)是否包含了需要的依赖。 5. 检查是否正确使用了service对象:在使用其他文件的service时,需要确保正确调用了service对象的方法,并且传入了正确的参数。可以检查调用service方法的代码是否正确。 请注意,以上是一些常见的解决方法,具体解决方法可能因具体情况而异。如果以上方法都无法解决问题,请提供更多的代码和错误信息,以便更好地帮助您解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值