• SpringBoot读取Yml中基本数据类型、List、Map、数组数据


    SpringBoot读取Yml中基本数据类型、List、Map、数组数据

    目录

    SpringBoot读取Yml中基本数据类型、List、Map、数组数据

    1、概述        

    2、读取基本数据类型

    3.1、读取对象List

    3.2、读取String的List集合

    4、读取数组

    5、 读取Map数据


    1、概述        

            在实际项目开发过程中,经常需要读取yml或者properties配置的数据,以yml配置文件为例,接下来将演示如何读取基本数据类型、List、Map、数组数据。

    pom文件需要添加依赖

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-configuration-processor</artifactId>
    4. <version>2.2.1.RELEASE</version>
    5. </dependency>

    2、读取基本数据类型

    yml配置:

    1. custom:
    2. string:
    3. value: test

    代码实现:

    使用@Value注解即可

    1. @Value("${custom.string.value}")
    2. private String stringValue;

    结果:

    3.1、读取对象List

    yml配置

    1. custom:
    2. beanlist:
    3. value:
    4. - name: 张三
    5. age: 18
    6. - name: 李四
    7. age: 20
    8. - name: 王五
    9. age: 22

            这里需要注意的是,定义list集合不能用@value注解来获取list集合的值,需要定义一个配置类bean,然后使用@ConfigurationProperties注解来获取list集合值,做法如下:

    User实体类

    1. @Data
    2. @NoArgsConstructor
    3. @AllArgsConstructor
    4. public class User{
    5. public String name;
    6. public Integer age;
    7. }

    配置类

    1. @Data
    2. @Component
    3. @ConfigurationProperties(prefix = "custom.beanlist")
    4. public class CustomBeanListConfig {
    5. List value;
    6. }

    用法:

    1. @Autowired
    2. private CustomBeanListConfig customBeanListConfig;
    3. public void test() {
    4. System.out.println("读取对象List:" + customBeanListConfig.getValue());
    5. }

    结果:

    3.2、读取String的List集合

    yml配置

    1. custom:
    2. list:
    3. value:
    4. - 1
    5. - 2
    6. - 3

     定义配置bean使用@ConfigurationProperties注解获取对象集合值。

    创建配置类

    1. @Data
    2. @Component
    3. @ConfigurationProperties(prefix = "custom.list")
    4. public class CustomListConfig {
    5. List value;
    6. }

    用法:

    1. @Autowired
    2. private CustomListConfig customListConfig;
    3. public void test() {
    4. System.out.println("读取List:" + customListConfig.getValue());
    5. }

    结果:

    4、读取数组

    yml配置

    1. custom:
    2. array:
    3. value: 1,2,3

    用法:

    1. @Value("${custom.array.value}")
    2. private String[] arrayValue;
    3. public void test() {
    4. System.out.println("读取数组:");
    5. Arrays.stream(arrayValue).forEach(s -> System.out.println(s));
    6. }

    结果:

    5、 读取Map数据

    yml配置

    1. custom:
    2. map:
    3. value: {name: 张三,age: 99}

     定义配置bean使用@ConfigurationProperties注解获取对象集合值。

    创建配置类

    1. @Data
    2. @Component
    3. @ConfigurationProperties(prefix = "custom.map")
    4. public class CustomMapConfig {
    5. Map<String,String> value;
    6. }

    用法:

    1. @Autowired
    2. private CustomMapConfig customMapConfig;
    3. public void test() {
    4. System.out.println("读取Map:");
    5. System.out.println(customMapConfig.toString());
    6. }

    结果:

  • 相关阅读:
    SpringBoot中日志的使用log4j2
    【iOS开发】iOS App的加固保护原理:使用ipaguard混淆加固
    用 pytorch 训练端对端验证码识别神经网络并进行 C++ 移植
    学习 vite + vue3 + pinia + ts(-)项目创建vs一些改变
    2022钉钉杯初赛A题(银行卡电信诈骗危险预测)
    hologres基础知识一文全
    基于selenium的pyse自动化测试框架
    web项目相关问题
    【2023米哈游-2】数组相关
    ABP集成SqlSugar
  • 原文地址:https://blog.csdn.net/shadowcw/article/details/126467490