SpringBoot中自定义properties文件配置参数并带有输入提示

1. 创建配置类

在项目中创建一个参数映射类如下

@ConfigurationProperties(prefix = "user.info")
public class MyProperties {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age= age;
    }
}

2. 编写提示信息

在项目中按此路径创建一个json文件 
resources/META-INF/spring-configuration-metadata.json

 

{
  "hints": [
    {
      "name": "user.info.age",
      "values": [
        {
          "value": 18
        },
        {
          "value": 20
        }
      ]
    }
  ],
  "groups": [
    {
      "sourceType": "org.xavier.config.properties.MyProperties",
      "name": "随便乱取",
      "type": "org.xavier.config.properties.MyProperties"
    }
  ],
  "properties": [
    {
      "sourceType": "org.xavier.config.properties.MyProperties",
      "name": "user.info.Name",
      "type": "java.lang.String"
    },
    {
      "sourceType": "org.xavier.config.properties.MyProperties",
      "name": "user.info.age",
      "type": "java.lang.Integer"
    }
  ]
}

记得重新编译项目,编译后才生效

3. 编写配置信息

在application.properties 里就可以给配置项赋值了,例如:

user.info.name=张三

user.info.age=18


posted @ 2019-03-20 12:42  netcorner  阅读(4474)  评论(0编辑  收藏  举报