YAML的舞蹈:掌握Spring整合YAML配置的技术细节

简介: YAML的舞蹈:掌握Spring整合YAML配置的技术细节


YML 介绍

YML(YAML)和 Properties 是两种常见的配置文件格式,用于在软件开发中存储和管理配置信息。它们有以下区别:

  1. 语法结构:YML 使用缩进和冒号的方式表示层级关系,而 Properties 使用键值对的方式表示配置项。
  2. 可读性:YML 的语法更加简洁和易读,采用了自然语言的表达方式,支持列表、字典等数据结构,使得配置文件更加直观。相比之下,Properties 的语法相对简单,只能表示简单的键值对。
  3. 层级结构:YML 支持多层级的配置结构,可以使用缩进来表示不同的层级关系,使得配置文件更加灵活。而 Properties 只支持一级的键值对结构,无法表示复杂的层级关系。
  4. 数据类型:YML 支持多种数据类型,包括字符串、整数、浮点数、布尔值等,且可以通过引号来表示特殊字符。Properties 则只支持字符串类型,所有的值都被视为字符串。
  5. 注释:YML 支持注释,可以在配置文件中添加注释来解释配置项的作用,提高可读性。而 Properties 不支持注释,无法在配置文件中添加解释性文字。

总的来说,YML 配置文件相对于 Properties 配置文件更加灵活、易读,并且支持多种数据类型和层级结构。在复杂的配置场景下,使用 YML 可以更好地组织和管理配置信息。而 Properties 则适用于简单的键值对配置,或者对配置文件格式要求较为严格的情况。

整合 Demo

引入依赖

<dependency>
 <groupId>org.yaml</groupId>
 <artifactId>snakeyaml</artifactId>
 <version>1.23</version>
</dependency>

创建配置文件

src/main/resources/account.yml

account:
  name: XW
  password: 123456

创建配置类

package world.xuewei.mybatis.config;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import java.util.Properties;
/**
 * 配置类
 *
 * @author 薛伟
 * @since 2023/9/14 20:51
 */
@Configuration
public class YmlConfig {
    @Bean
    public PropertySourcesPlaceholderConfigurer configurer() {
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(new ClassPathResource("account.yml"));
        Properties properties = yamlPropertiesFactoryBean.getObject();
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setProperties(properties);
        return configurer;
    }
}

world.xuewei.mybatis.entity.Account

package world.xuewei.mybatis.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.Serializable;
/**
 * 用户实体
 *
 * @author 薛伟
 * @since 2023/9/14 20:51
 */
@Component
public class Account implements Serializable {
    private Integer id;
    @Value("${account.name}")
    private String name;
    @Value("${account.password}")
    private String password;
    public Account() {
    }
    public Account(Integer id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

测试程序

package world.xuewei;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import world.xuewei.mybatis.entity.Account;
import world.xuewei.mybatis.service.AccountService;
/**
 * @author 薛伟
 * @since 2023/9/14 20:51
 */
public class Dao1Test {
    private ApplicationContext context;
    @Before
    public void before() {
        // 指定配置文件,创建 Spring 工厂
        context = new AnnotationConfigApplicationContext("world.xuewei.mybatis");
    }
    @Test
    public void test1() {
        Account account = context.getBean("account", Account.class);
        System.out.println(account);
    }
}

注意当前的这种集成方案还是无法注入 YML 中的集合类型!



相关文章
|
6天前
|
存储 Java 数据安全/隐私保护
|
6天前
|
安全 Java 开发者
深入理解Spring Boot配置绑定及其实战应用
【4月更文挑战第10天】本文详细探讨了Spring Boot中配置绑定的核心概念,并结合实战示例,展示了如何在项目中有效地使用这些技术来管理和绑定配置属性。
16 1
|
6天前
|
XML Java 数据格式
Spring高手之路18——从XML配置角度理解Spring AOP
本文是全面解析面向切面编程的实践指南。通过深入讲解切面、连接点、通知等关键概念,以及通过XML配置实现Spring AOP的步骤。
23 6
Spring高手之路18——从XML配置角度理解Spring AOP
|
6天前
|
消息中间件 开发框架 Java
什么是Spring Boot 自动配置?
Spring Boot 是一个流行的 Java 开发框架,它提供了许多便利的功能和工具,帮助开发者快速构建应用程序。其中一个最引人注目的特性是其强大的自动配置功能。
11 0
|
6天前
|
Java Spring
Spring文件配置以及获取
Spring文件配置以及获取
13 0
|
6天前
|
Java 微服务 Spring
Spring Boot中获取配置参数的几种方法
Spring Boot中获取配置参数的几种方法
22 2
|
6天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
22 1
|
6天前
|
Java 开发者 Spring
Spring Boot中的资源文件属性配置
【4月更文挑战第28天】在Spring Boot应用程序中,配置文件是管理应用程序行为的重要组成部分。资源文件属性配置允许开发者在不重新编译代码的情况下,对应用程序进行灵活地配置和调整。本篇博客将介绍Spring Boot中资源文件属性配置的基本概念,并通过实际示例展示如何利用这一功能。
27 1
|
6天前
|
Java Spring 容器
如何用基于 Java 配置的方式配置 Spring?
如何用基于 Java 配置的方式配置 Spring?
|
6天前
|
存储 前端开发 Java
第十一章 Spring Cloud Alibaba nacos配置中心
第十一章 Spring Cloud Alibaba nacos配置中心
30 0