欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > springboot给属性赋值的两种方式(yaml与properties)

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

2024/10/25 5:23:12 来源:https://blog.csdn.net/qq_44858608/article/details/140639574  浏览:    关键词: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提供了更强的表达能力和更好的可读性。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com