【springboot】yaml文件配置提示

1.效果示例

在这里插入图片描述

2.问题说明
1.自定义的starter或者模块中的属性配置类在配置文件中给予提示
2.配置类中要有注释说明,且要有set\get方法,直接加@Data注解方便
3.将maven项目clean后install,在target\classes\META-INF下会生成spring-configuration-metadata.json文件,将该文件拷贝到src\main\resources\META-INF下,删除target下的,打包的时候可以去掉依赖包
4.json文件中可以加hints来说明可选值
3.pom依赖配置
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <!--是否向下传递-->
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
4.配置类
package com.learning.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Author wangyouhui
 * @Description TODO
 **/
@Component("testProperties")
@ConfigurationProperties(prefix = "tool")
@Data
public class TestProperties {
    /**
     * 提示
     */
    private String tip;

    /**
     * 模式
     */
    private String model;
}
5.spring-configuration-metadata.json
{
  "groups": [
    {
      "name": "tool",
      "type": "com.learning.config.TestProperties",
      "sourceType": "com.learning.config.TestProperties"
    }
  ],
  "properties": [
    {
      "name": "tool.model",
      "type": "java.lang.String",
      "description": "模式",
      "sourceType": "com.learning.config.TestProperties"
    },
    {
      "name": "tool.tip",
      "type": "java.lang.String",
      "description": "提示",
      "sourceType": "com.learning.config.TestProperties"
    }
  ],
  "hints": [
    {
      "name": "tool.model",
      "values": [
        {
          "value": "detail",
          "description": "详情模式."
        },
        {
          "value": "simple",
          "description": "极简模式."
        }
      ]
    }
  ]
}
6.项目结构截图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佑辉

老板,赏点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值