springboot给属性赋值的两种方式(yaml与properties)

一,介绍

        在Spring Boot中,配置文件是用来设置应用程序的各种参数和操作模式的重要部分。Spring Boot支持两种主要类型的配置文件:properties文件和YAML 文件。这两种文件都可以用来定义相同的配置,但它们在格式和表达能力上有所不同。

二,Properties 配置方式

  properties文件是Java平台最传统的配置方式,文件扩展名为 .properties。这种格式非常简单,主要由键值对组成,每一对键值对设置一个配置属性。
示例:
定义模型Person类:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="person")
public class Person {
    private String name;
    private int age;
    private String uuid;
    private Dog dog;

    // standard getters and setters

    public static class Dog {
        private String name;
        private String breed;

        // standard getters and setters
    }
}
Properties 配置
person.name=John Doe
person.age=35
person.uuid=${random.uuid}
person.dog.name=Rex
person.dog.breed=Labrador
         这样配置后,Spring Boot 会自动application.properties中的相关配置注入到 Person对象和其内部的 Dog对象。
使用 @Value注解也可以直接在 Spring Boot 应用中注入配置值,例
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Person {
    @Value("${person.name}")
    private String name;

    @Value("${person.age}")
    private int age;

    @Value("${person.uuid}")
    private String uuid;

    // 内部类和其他配置略
}

三,YAML 配置方式

  YAML 是一种层次结构化的数据格式,相比于 properties文件,它支持列表和嵌套的对象,使得配置更加清晰和组织化。
        yaml配置:
person:
  name: "John Doe"
  age: 35
  uuid: ${random.uuid}
  dog:
    name: "Rex"
    breed: "Labrador"
        这时要将YAML文件中的配置自动映射到一个Java类中,需要在Spring Boot应用中定义相应的配置类,并使用@ConfigurationProperties注解。
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Configuration
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private int age;
    private String uuid;
    private Dog dog;


    @Component
    public static class Dog {
        private String name;
        private String breed;

        // getters and setters
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getBreed() {
            return breed;
        }

        public void setBreed(String breed) {
            this.breed = breed;
        }
    }
}

四,对比

1. 可读性

  • YAML 由于其支持层级结构,通常在表达更复杂的配置时更加清晰和易读。
  • Properties 文件更适合简单的平面键值对,但在需要表达嵌套配置时可读性较差。

2. 表达能力

  • YAML 支持复杂的数据结构,如列表和字典(即嵌套的对象),这使得它在表达如安全规则、路由配置等复杂配置时非常有用。
  • Properties 文件不支持直接的层级或复杂结构,所有结构都必须通过点分隔的方式平铺开来表达。

3. 错误检测

  • YAML 文件由于格式更加复杂,对缩进非常敏感,错误的缩进可能导致整个文件无法解析。
  • Properties 文件结构简单,缩进和格式错误的容忍度较高。

4. 使用场景

  • 如果配置较为简单,或是迁移遗留项目而不希望引入新的复杂性,那么使用.properties可能更合适。
  • 对于新项目或需要表达复杂配置的情况,.yaml提供了更强的表达能力和更好的可读性。
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xwzzz_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值