Spring Boot 项目中读取 YAML 文件中的数组、集合和 HashMap

在 Spring Boot 项目中,我们经常使用 YAML 文件来配置应用程序的属性。在这篇博客中,我将模拟如何在 Java 的 Spring Boot 项目中读取 YAML 文件中的数组、集合和 HashMap。

1. 介绍

YAML(YAML Ain't Markup Language)是一种人类可读的数据序列化格式,通常用于配置文件。在 Spring Boot 中,我们可以使用 @ConfigurationProperties 注解和 application.yml 文件轻松管理应用程序的配置。

2. 准备工作

首先,确保你的 Spring Boot 项目中包含以下依赖:

复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

3. 创建 YAML 文件

src/main/resources 目录下创建一个名为 application.yml 的文件,并添加以下内容:

复制代码
server:
  port: 6666
lps:
  languages:
    - Java
    - Python
    - C
    - C++
    - C#
  colors:
    - red
    - green
    - blue
  fruits:
    - name : apple
      price : 3.88
    - name : banana
      price : 2.88
    - name : orange
      price : 5.28
  ages:
    john: 30
    alice: 25
    bob: 35

这个文件包含了两个数组、一个水果列表和一个年龄的 HashMap。

4. 创建配置类

创建一个 Java 配置类,用于将 YAML 文件中的属性映射到对象。创建一个名为 AppProperties.java 的文件:

java 复制代码
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;

/**
 * @author 阿水
 * @ClassName LpsProperties 
 * @description: TODO
 * @date 2023年11月26日
 * @version: 1.0
 */
@Component
@ConfigurationProperties(prefix = "lps")
@Data
public class LpsProperties {
    private String[] languages;
    private List<String> colors;
    private List<Fruits> fruits;
    private Map<String, Integer> ages;
    @Data
    public static class Fruits {
        private String name;
        private Double price;
    }
}

5. 注入配置类

在你的服务或组件中,通过 @Autowired 注解将 AppProperties 配置类注入,然后访问其中的属性:

java 复制代码
 @Autowired
 LpsProperties lpsProperties;

6. 运行项目

在你的应用程序中使用 LpsProperties类,并调用方法,你将能够从 YAML 文件中读取数组、集合和 HashMap,并在控制台上输出它们。

java 复制代码
@SpringBootTest
class CountDemoApplicationTests {
    @Autowired
    LpsProperties lpsProperties;

    @Test
    void contextLoads() {
        String[] languages = lpsProperties.getLanguages();
        System.out.print("常见编程语言:");
        for (String language : languages) {
            System.out.print(language + " ");
        }
        System.out.println("等等");
        System.out.println("----------------------");
        Map<String, Integer> ages = lpsProperties.getAges();
        ages.forEach((key, value) -> System.out.println(key + "今年" + value + "岁"));
        System.out.println("----------------------");
        List<String> colors = lpsProperties.getColors();
        for (String color : colors) {
            System.out.println("收到的颜色为:" + color);
        }
        System.out.println("----------------------");
        List<LpsProperties.Fruits> fruits = lpsProperties.getFruits();
        fruits.forEach(fruit -> System.out.println(fruit));

    }

}

7. 结论

通过使用 @ConfigurationProperties 注解和 YAML 文件,我们可以轻松地将配置属性映射到 Java 对象中。这使得在 Spring Boot 项目中管理数组、集合和 HashMap 的配置变得简单而直观。

希望这篇博客对你在 Spring Boot 项目中读取 YAML 文件中的数组、集合和 HashMap 有所帮助。如有疑问或建议,请随时提出。

相关推荐
Blossom.1186 分钟前
Prompt工程与思维链优化实战:从零构建动态Few-Shot与CoT推理引擎
人工智能·分布式·python·智能手机·django·prompt·边缘计算
计算机毕设VX:Fegn089512 分钟前
计算机毕业设计|基于springboot + vue宠物医院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
zzcufo35 分钟前
使用multimonitortool工具实现多屏显示器的映射
windows·计算机外设
一条咸鱼¥¥¥1 小时前
【运维经验】服务器磁盘做镜像的方法
运维·服务器·windows·经验分享
cpp_learners1 小时前
Qt Windows版本的《智能鼠标点击器》
windows·qt·鼠标点击器
love530love2 小时前
Windows 11 下 Z-Image-Turbo 完整部署与 Flash Attention 2.8.3 本地编译复盘
人工智能·windows·python·aigc·flash-attn·z-image·cuda加速
毕设源码-钟学长2 小时前
【开题答辩全过程】以 个性化电影推荐网站的设计与实现为例,包含答辩的问题和答案
java·spring boot
MediaTea2 小时前
Python:模块 __dict__ 详解
开发语言·前端·数据库·python
qq_2704900962 小时前
SpringBoot药品管理系统设计实现
java·spring boot·后端
jarreyer2 小时前
python,numpy,pandas和matplotlib版本对应关系
python·numpy·pandas