关于yml配置文件中,配置键名不能为大写

前景

在项目中需要加入自定义的表结构配置,所以想到了使用yml加载 然后写了以下yml配置代码

tableStruct:
  device:
    gaid: STRING
    system_version: STRING
    operator_name: STRING
    device_brand: STRING
    device_model: STRING
    memory_size: STRING
    space_size: STRING
    remain_memory_size: STRING
    remain_space_size: STRING
    cpu_name: STRING
    gles_version: STRING
    screen_resolution: STRING
    screen_density: STRING
    tz: INTEGER
    device_category: STRING
    device_language: STRING
    is_first_open: INTEGER
    user_first_touch_timestamp: INTEGER
    continent: STRING
    country: STRING
    app_version: STRING

本来是要配置到下面这个类的,该yml文件为Springboot的yml配置文件application.yml。按道理说,通过下面的类配置就可以成功加载了

@Component
@ConfigurationProperties(prefix = "tableStruct")
public class TableStruct {
          
   
    private Map<String, String> device;
}

但是但是!!!!! 这个时候会一直报错,其中指定字符S有问题,需要我检查yml配置文件格式。 因为我不知道这个问题,所以尝试了很多方式,最开始以为是我的配置项STRING出了问题,然后我又改成了加上单引号的STRING结果还是不行。

最后知道了,是配置项的键名tableStruct的S出了问题,这里键名不能大写,可改为table-struct的形式。 另外,我试了改成下划线也会报错table_struct 同时,修改类的注解使用的加载前缀,ok!

经验分享 程序员 微信小程序 职场和发展