码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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. }

    结果:

  • 相关阅读:
    解决Java 8 date/time type `java.time.LocalDateTime` not supported by default
    基于XML的Web服务Java接口(JAX-WS)、Jakarta XML Web Services Eclipse 实现
    “富二代”极氪,辉煌过后总要独自长大
    Web上的推箱子游戏Sokoban
    C# 学习之路(C# 的概念)
    基于Python和mysql开发的智慧校园答题考试系统(源码+数据库+程序配置说明书+程序使用说明书)
    云原生Kubernetes:K8S集群使用带凭证的harbor仓库
    鸿蒙应用开发之Hello World-1(续)
    技术人员怎样提升对业务的理解
    【技术积累】Linux中的命令行【理论篇】【十】
  • 原文地址:https://blog.csdn.net/shadowcw/article/details/126467490
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号