当前位置: 首页 > news >正文

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 setterspublic 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: 35uuid: ${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;@Componentpublic static class Dog {private String name;private String breed;// getters and setterspublic 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提供了更强的表达能力和更好的可读性。
http://www.lryc.cn/news/405851.html

相关文章:

  • 20240725 每日AI必读资讯
  • 17_高级进程间通信 UNIX域套接字1
  • 大型语言模型的生物医学知识图优化提示生成
  • winform datagrid 全部勾选
  • 从 NextJS SSRF 漏洞看 Host 头滥用所带来的危害
  • LC617-合并二叉树
  • 深入解析:端到端目标检测模型的奥秘
  • xmind--如何快速将Excel表中多列数据,复制到XMind分成多级主题
  • 在 Android 上实现语音命令识别:详细指南
  • 怎么理解FPGA的查找表与CPLD的乘积项
  • 51.2T 800G 以太网交换机,赋能AI开放生态
  • 【制作100个unity游戏之31】用unity制作一个爬坡2d赛车小游戏
  • Spring Boot 注解 @PostConstruct 介绍
  • 深度学习环境配置报错解决日记
  • 百度,有道,谷歌翻译API
  • java-双亲委派机制
  • 【C++】set的使用
  • React 18【实用教程】(2024最新版)
  • Perl语言入门学习指南
  • 《Java8函数式编程》学习笔记汇总
  • C语言之封装,继承,多态
  • GO内存分配详解
  • 每日Attention学习12——Exterior Contextual-Relation Module
  • 为什么现在电销公司这么难?
  • 每天一个数据分析题(四百四十二)- 标签与指标
  • [论文笔记] pai-megatron-patch Qwen2-72B/7B/1.5B 长文本探路
  • 【SpringCloud】微服务远程调用OpenFeign
  • MySQL零散拾遗(四)
  • 大语言模型-检索测评指标
  • Zookeeper集群中节点之间数据是如何同步的