1、简介
在springbooot中properties文件类型作为最常用的配置文件格式之一,常用的简单类型不用多说,主要了解下properties中的各种复杂类型(List、Map类型及所包含的复杂泛型)的配置,使得能够更加丰富我们开发中配置文件的数据结构,包括:
List<实体类>、List<Map<String,Object>>、List<List<String>>、List<List<实体类>>、List<Map<String,实体类>>、Map<String,实体类>、Map<String,List<Object/String>>、Map<String,List<实体类>>
等。
2、介绍及测试
本测试使用最简单的springboot web项目做测试springboot版本为2.3.4,properties配置为了方便直接放在application.properties中,测试项目结构如下图,且测试代码直接写到ApplicationRunner的实现类中,项目启动时直接执行测试类。
测试pom文件如下:
2.1.基础list和map类型
2.1.1.properties配置
# properties中简单的maptest.coll.maps.key1=value1
test.coll.maps.key2=value2
# properties中简单的list
test.coll.list[0]=value0
test.coll.list[1]=value1
除却上面配置方式外还有本人感觉更简单且好理解的配置方式:
# properties中简单的list
test.coll.list=value0,value2
# properties中简单的map
test.coll.maps[key1]=value1
test.coll.maps[key2]=value2
2.1.2.配置加载
@Slf4j@Configuration@ConfigurationProperties(prefix ="test.coll")@DatapublicclassTestConf01{privateMap<String,Object> maps;privateList<String> list;}
2.1.3.执行
@Slf4j@Order(1)@ComponentpublicclassTestRunner01implementsApplicationRunner{@AutowiredprivateTestConf testConf;@Overridepublicvoidrun(ApplicationArguments args){// 测试简单map类型
val mapsJson =JSON.toJSONString(testConf.getMaps());
log.info("这是简单map类型:{}", mapsJson);// 测试简单list类型
val listJson =JSON.toJSONString(testConf.getList());
log.info("这是简单list类型:{}", listJson);}}
2.2.复杂List
本节主要讲解复杂性list配置,包括
List<实体类>、List<Map<String,Object>>、List<List<String>>、List<List<实体类>>、List<Map<String,实体类>>
等,这些类型对list在配置文件中的应用已经够用,如需更加复杂的配置,我们按照文档中例中的简单到复杂,一维到二维的规律继续推演即可。
2.2.1.properties配置
# properties中复杂类型list,泛型为实体类(List<Person>)
test.coll.list.list-person[0].name=tom
test.coll.list.list-person[0].age=18
test.coll.list.list-person[1].name=jerry
test.coll.list.list-person[1].age=20
# properties中复杂类型list,泛型为Map(List<Map<String,Object>>)
test.coll.list.list-map[0].key1=value1
test.coll.list.list-map[0].key2=value2
test.coll.list.list-map[1].key2=value2
test.coll.list.list-map[1].key3=value3
# properties中复杂类型list,泛型为List(List<List<String>>)
test.coll.list.list-list[0][0]=value1
test.coll.list.list-list[0][1]=value2
test.coll.list.list-list[1][0]=value3
test.coll.list.list-list[1][1]=value4
# properties中复杂list,list中为List<实体类>
test.coll.list.list-list-person[0][0].name=tom1
test.coll.list.list-list-person[0][0].age=18
test.coll.list.list-list-person[0][1].name=jerry1
test.coll.list.list-list-person[0][1].age=20
test.coll.list.list-list-person[1][0].name=tom2
test.coll.list.list-list-person[1][0].age=18
test.coll.list.list-list-person[1][1].name=jerry2
test.coll.list.list-list-person[1][1].age=20
# properties中复杂list,list中为Map<String,实体类>>
test.coll.list.list-map-person[0].person1[name]=tom1
test.coll.list.list-map-person[0].person1[age]=18
test.coll.list.list-map-person[0].person2[name]=jerry1
test.coll.list.list-map-person[0].person2[age]=20
test.coll.list.list-map-person[1].person3[name]=tom2
test.coll.list.list-map-person[1].person3[age]=18
test.coll.list.list-map-person[1].person4[name]=jerry2
test.coll.list.list-map-person[1].person4[age]=20
2.2.2.配置加载
@Slf4j@Configuration@ConfigurationProperties(prefix ="test.coll.list")@DatapublicclassTestConf02{// 复杂类型list,泛型为实体类(List<Person>)privateList<Person> listPerson;// 复杂类型list,泛型为Map(List<Map<String,Object>>)privateList<Map<String,Object>> listMap;// 复杂类型list,泛型为List(List<List<String>>)privateList<List<String>> listList;// 复杂list,list中为List<实体类>privateList<List<Person>> listListPerson;// 复杂list,list中为Map<String,实体类>>privateList<Map<String,Person>> listMapPerson;@DatapublicstaticclassPerson{privateString name;privateint age;}}
2.1.3.执行
@Slf4j@Order(2)@ComponentpublicclassTestRunner02implementsApplicationRunner{@AutowiredprivateTestConf02 testConf;@Overridepublicvoidrun(ApplicationArguments args){// 复杂类型list,泛型为实体类(List<Person>)
val json1 =JSON.toJSONString(testConf.getListPerson());
log.info("复杂list,list中为实体类:{}", json1);// 复杂类型list,泛型为Map(List<Map<String,Object>>)
val json2 =JSON.toJSONString(testConf.getListMap());
log.info("复杂list,list中为Map:{}", json2);// 复杂类型list,泛型为List(List<List<String>>)
val json3 =JSON.toJSONString(testConf.getListList());
log.info("复杂list,list中为List<String>:{}", json3);// 复杂类型list,泛型为List(List<List<Person>>)
val json4 =JSON.toJSONString(testConf.getListListPerson());
log.info("复杂list,list中为List<实体类>:{}", json4);// 复杂类型list,泛型为List(List<Map<String,Person>>)
val json5 =JSON.toJSONString(testConf.getListMapPerson());
log.info("复杂list,list中为Map<String,实体类>:{}", json5);}}
2.3.复杂Map
本节主要讲解复杂性list配置,包括
Map<String,实体类>、Map<String,List<Object/String>>、Map<String,List<实体类>>
讲解和实例
2.3.1.properties配置
#-------------------------------------------------------Map复杂类型-----------------------------------------------------
# Map<String, 实体类>
test.coll.map.map-person.person1[name]=tom
test.coll.map.map-person.person1[age]=18
test.coll.map.map-person.person2[name]=jerry
test.coll.map.map-person.person2[age]=20
# Map<String,List<Object/String>>
test.coll.map.map-list.key1[0]=value1
test.coll.map.map-list.key1[1]=value2
test.coll.map.map-list.key2[0]=value3
test.coll.map.map-list.key2[1]=value4
# Map<String,List<实体类>>,
# test.coll.map.map-list-person.Map中的key[list类型value的下标].实体类的属性名=tom1
test.coll.map.map-list-person.person1[0].name=tom1
test.coll.map.map-list-person.person1[0].age=18
test.coll.map.map-list-person.person1[1].name=jerry1
test.coll.map.map-list-person.person1[1].age=20
test.coll.map.map-list-person.person2[0].name=tom2
test.coll.map.map-list-person.person2[0].age=18
test.coll.map.map-list-person.person2[1].name=jerry2
test.coll.map.map-list-person.person2[1].age=20
2.2.2.配置加载
@Slf4j@Configuration@ConfigurationProperties(prefix ="test.coll.map")@DatapublicclassTestConf03{// Map<String,Person>privateMap<String,TestConf02.Person> mapPerson;// Map<String,List<Object>>privateMap<String,List<Object>> mapList;// Map<String,List<Person>>privateMap<String,List<TestConf02.Person>> mapListPerson;}
2.1.3.执行
@Slf4j@Order(3)@ComponentpublicclassTestRunner03implementsApplicationRunner{@AutowiredprivateTestConf03 testConf;@Overridepublicvoidrun(ApplicationArguments args){// 泛型为实体类:Map<String,Person>
val json1 =JSON.toJSONString(testConf.getMapPerson());
log.info("复杂Map,Map<String,实体类>:{}", json1);// 泛型为实体类:Map<String,List<Object>
val json2 =JSON.toJSONString(testConf.getMapList());
log.info("复杂Map,Map<String,List<Object>:{}", json2);// 泛型为实体类:Map<String,List<Person>
val json3 =JSON.toJSONString(testConf.getMapListPerson());
log.info("复杂Map,Map<String,List<Person>:{}", json3);}}
版权归原作者 Timmer丿 所有, 如有侵权,请联系我们删除。