SpringBoot 读取 properties文件配置

一、Map 和 List

1.1、.properties 文件map类型和list的写法

# properties 配置类

# Map<String,String> 结构方式一
map.data.map-config.name-one=java
map.data.map-config.name-two=c
map.data.map-config.name-three=python

# Map<String,String> 结构方式二【推荐】
map.data.map-config[name-one]=java
map.data.map-config[name-two]=c
map.data.map-config[name-three]=python

# list 第一种方式
map.data.list-config[0]=apollo
map.data.list-config[1]=apollo1
map.data.list-config[2]=apollo2

# list 第二种方式(推荐)
map.data.listConfig=apollo,apollo1,apollo2

1.2、 代码读取配置实现

// Map 和 list 结构方式一
/**
 * 配置类
 * @author 
 * @date 2022/9/16 10:30
 */
@Slf4j
@Configuration
@Data
@ConfigurationProperties(prefix = "map.data")
public class Config {
    Map<String, String> mapConfig;

    List<String> listConfig;
}
/**
 * 测试类
 * @author 
 * @date 2022/9/16 17:35
 */
@Slf4j
class MapConfigTest extends BaseTest{

    @Autowired
    Config configPro;

    @Test
    void mapConfigProTest() {
        Map<String, String> mapConfig = configPro.getMapConfig();
        log.info("Map 结构方式一获取的值为:{}",  JSON.toJSONString(mapConfig));
        System.out.println("Map 结构方式一获取的值为:" + JSON.toJSONString(mapConfig));

        List<String> listConfig = configPro.getListConfig();
        log.info("list 结构一获取的值为:{}", JSON.toJSONString(listConfig));
        System.out.println("list 结构一获取的值为:{}" + JSON.toJSONString(listConfig));
    }
}

// 结果
Map<String,String> 结构方式一获取的值为:{"name-one":"java","name-two":"c","name-three":"python"}

list<String> 结构一获取的值为:["apollo","apollo1","apollo2"]

二、Map> 和 List>

# properties文件配置

# Map<String, List<String>> mapListConfig
data.map-list-config.key0[0]=0
data.map-list-config.key0[1]=1
data.map-list-config.key1[0]=10
data.map-list-config.key1[1]=11

# List<Map<String, String>> listMapConfig
data.list-map-config[0].key0=0
data.list-map-config[0].key00=00
data.list-map-config[1].key1=1
data.list-map-config[1].key11=11
/**
 * @author
 * @date 2022/9/16 10:30
 */
@Slf4j
@Configuration
@Data
@ConfigurationProperties(prefix = "data")
public class Config {
    Map<String, List<String>> mapListConfig;

    List<Map<String, String>> listMapConfig;
}

/**
 * @author
 * @date 2022/9/16 17:35
 */
@Slf4j
class MapConfigTest extends BaseTest{

    @Autowired
    Config configPro;

    @Test
    void mapConfigProTest() {
        Map<String, List<String>> mapListConfig = configPro.getMapListConfig();
        log.info("Map> 结构方式一获取的值为:{}",  JSON.toJSONString(mapListConfig));
        System.out.println("Map> 结构方式一获取的值为:" + JSON.toJSONString(mapListConfig));

        List<Map<String, String>> listMapConfig = configPro.getListMapConfig();
        log.info("List> 结构一获取的值为:{}", JSON.toJSONString(listMapConfig));
        System.out.println("List> 结构一获取的值为:{}" + JSON.toJSONString(listMapConfig));
    }
}
// 结果
Map<String, List<String>> 结构方式一获取的值为:{"key0":["0","1"],"key1":["10","11"]}

List<Map<String, String>> 结构一获取的值为:[{"key0":"0","key00":"00"},{"key1":"1","key11":"11"}]

三、Map> 和 List>

# properties 配置

# Map<String, List<Object>>
data.map-list-config.key0[0].name=xiao
data.map-list-config.key0[0].type=1
data.map-list-config.key0[1].name=hua
data.map-list-config.key0[1].type=2
data.map-list-config.key1[0].name=li
data.map-list-config.key1[0].type=3
data.map-list-config.key1[1].name=ming
data.map-list-config.key1[1].type=4

# list<Map<String, Object>>
data.list-map-config[0].key0.name=huang
data.list-map-config[0].key0.type=tang
data.list-map-config[0].key1.name=lan
data.list-map-config[0].key1.type=tian
data.list-map-config[1].key0.name=bai
data.list-map-config[1].key0.type=yun
/**
 * @author 
 * @date 2022/9/16 10:30
 */
@Slf4j
@Configuration
@Data
@ConfigurationProperties(prefix = "data")
public class Config {
    Map<String, List<Entity>> mapListConfig;

    List<Map<String, Entity>> listMapConfig;

    @Data
    public static class Entity {
        private String name;
        private String type;
    }
}
/**
 * @author 
 * @date 2022/9/16 17:35
 */
@Slf4j
class MapConfigTest extends BaseTest{

    @Autowired
    Config configPro;

    @Test
    void mapConfigProTest() {
        Map<String, List<Config.Entity>> mapListConfig = configPro.getMapListConfig();
        log.info("Map>获取的值为:{}",  JSON.toJSONString(mapListConfig));
        System.out.println("Map>获取的值为:" + JSON.toJSONString(mapListConfig));

        List<Map<String, Config.Entity>> listMapConfig = configPro.getListMapConfig();
        log.info("List>获取的值为:{}", JSON.toJSONString(listMapConfig));
        System.out.println("List>获取的值为:{}" + JSON.toJSONString(listMapConfig));
    }
}
// 结果
Map<String, List<Config.Entity>>获取的值为:{"key0":[{"name":"xiao","type":"1"},{"name":"hua","type":"2"}],"key1":[{"name":"li","type":"3"},{"name":"ming","type":"4"}]}

List<Map<String, Config.Entity>>获取的值为:[{"key0":{"name":"huang","type":"tang"},"key1":{"name":"lan","type":"tian"}},{"key0":{"name":"bai","type":"yun"}}]

参考文档

你可能感兴趣的:(SpringBoot,spring,boot,java)