一 使用 YAML 而不是 Properties

YAML是 JSON 的超集,因此,它是用于指定分层配置数据的便捷格式。只要 class 路径上有SnakeYAML library,SpringApplication class 就会自动支持 YAML 作为 properties 的替代。

如果使用“Starters”,则spring-boot-starter会自动提供 SnakeYAML。

1.1、加载 YAML

Spring Framework 提供了两个方便的 classes,可用于加载 YAML 文档。 YamlPropertiesFactoryBean将 YAML 加载为PropertiesYamlMapFactoryBean将 YAML 加载为Map

对于 example,请考虑以下 YAML 文档:

environments:
dev:
url: http://dev.example.com
name: Developer Setup
prod:
url: http://another.example.com
name: My Cool App

前面的 example 将转换为以下 properties:

environments.dev.url=http://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=http://another.example.com
environments.prod.name=My Cool App

YAML lists 用dereferencers 表示为 property 键。例如,请考虑以下 YAML:

my:
servers:
- dev.example.com
- another.example.com

前面的 example 将转换为这些 properties:

my.servers[0]=dev.example.com
my.servers[1]=another.example.com

要使用 Spring Boot 的Binder实用程序(这是@ConfigurationProperties所做的)绑定到 properties,你需要在java.util.List(或Set)类型的目标 bean 中有一个 property,你需要提供一个 setter 或者用一个 setter 初始化它。可变值。对于 example,以下 example 绑定到前面显示的 properties:

@ConfigurationProperties(prefix="my")
public class Config {

private List<String> servers = new ArrayList<String>();

public List<String> getServers() {
return this.servers;
}
}

1.2、在 Spring 环境中将 YAML 公开为 Properties

YamlPropertySourceLoader class 可用于在 Spring Environment中将 YAML 公开为PropertySource。这样做可以使用@Value annotation 和占位符语法来访问 YAML properties。

1.3、YAML 文件

您可以使用spring.profiles key 在单个文件中指定多个 profile-specific YAML 文档,以指示文档何时适用,如下面的示例所示:

server:
address: 192.168.1.100
---
spring:
profiles: development
server:
address: 127.0.0.1
---
spring:
profiles: production & eu-central
server:
address: 192.168.1.120

在前面的 example 中,如果development profile 是 active,则server.address property 是127.0.0.1。同样,如果production eu-central profiles 是 active,则server.address property 是192.168.1.120。如果未启用developmentproductioneu-central profiles,则 property 的 value 为192.168.1.100

因此,spring.profiles可以包含一个简单的 profile name(用于 example production)或 profile 表达式。对于 example production & (eu-central | eu-west),profile 表达式允许表达更复杂的 profile 逻辑。检查参考指南以获取更多详细信息。

如果 application context 启动时 none 显式为 active,则会激活默认的 profiles。因此,在下面的 YAML 中,我们在“默认”profile 中为spring.security.user.password设置了一个只有的值:

server:
port: 8000
---
spring:
profiles: default
security:
user:
password: weak

然而,在下面的示例中,始终设置密码,因为它没有附加到任何 profile,并且必须在必要时在所有其他 profiles 中显式重置:

server:
port: 8000
spring:
security:
user:
password: weak

使用spring.profiles元素指定的 Spring profiles 可以选择使用!字符否定。如果为单个文档指定了否定和 non-negated profiles,则至少一个 non-negated profile 必须 match,并且没有否定 profiles 可能 match。

1.4、YAML 缺点

无法使用@PropertySource annotation 加载 YAML files。因此,如果您需要以这种方式加载值,则需要使用 properties 文件。

二 YAML语法:

2.1、基本语法

k:(空格)v:表示一对键值对(空格必须有);

空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的

server:
port: 8081
path: /hello

属性和值也是大小写敏感;

2.2、值的写法

字面量:普通的值(数字,字符串,布尔)

k: v:字面直接来写;

字符串默认不用加上单引号或者双引号;

"":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思

name: "zhangsan \n lisi":输出;zhangsan 换行 lisi

'':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据

name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi

对象、Map(属性和值)(键值对):

k: v:在下一行来写对象的属性和值的关系;注意缩进

对象还是k: v的方式

friends:
lastName: zhangsan
age: 20

行内写法:

friends: {lastName: zhangsan,age: 18}

数组(List、Set):

用- 值表示数组中的一个元素

pets:
- cat
- dog
- pig

行内写法

pets: [cat,dog,pig]

SpringBoot配置文件之Yml语法的更多相关文章

  1. springboot配置文件之yml的语法学习

    springboot配置文件(.yml/.yaml.properties) YAML(YAML Ain't Markup Language) YAML A Markup Language:是一个标记语 ...

  2. 3 - 简单了解一下springboot中的yml语法 和 使用yml赋值

    1.简单了解yml语法 2.使用yml给实体类赋值 准备工作:导入依赖 <!-- 这个jar包就是为了实体类中使用@ConfigurationProperties(prefix = " ...

  3. 【yml】springboot 配置类 yml语法

    参考:https://www.runoob.com/w3cnote/yaml-intro.html YAML 是 "YAML Ain't a Markup Language"(YA ...

  4. springboot配置文件(.yml)中自定义属性值并在controller里面获取

    1,由于项目需要,学习了新的框架--springboot,顺便练习一下在.yml中配置自定义属性并在controller里面获取.(以下的Springboot框架我已经搭建好,就不在陈述) 2,spr ...

  5. SpringBoot学习笔记(三)——Springboot配置文件

    SpringBoot不像之前用spring+springMVC做项目的时候,他不需要配置大量的看上去很乱很复杂的xml配置文件.在SpringBoot中你可以通过java代码和注解配置项目,也可以通过 ...

  6. SpringBoot 配置文件存放位置及读取顺序

    SpringBoot配置文件可以使用yml格式和properties格式 分别的默认命名为:application.yml.application.properties 存放目录 SpringBoot ...

  7. [SpringBoot] - 配置文件的多种形式及JSR303数据校验

    Springboot配置文件: application.yml   application.properties(自带) yml的格式写起来稍微舒服一点 在application.properties ...

  8. SpringBoot配置(1) 配置文件application&yml

    SpringBoot配置(1) 配置文件application&yml 一.配置文件 1.1 配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的. application ...

  9. springboot的yaml基础语法与取值,配置类,配置文件加载优先级

    1.基本语法k:(空格)v:表示一对键值对(一个空格必须有):以空格的缩进来控制层级关系:只要是左对齐的一列数据,都是同一个层级的属性和值也是大小写敏感: server: port: 8081 pat ...

随机推荐

  1. Spring boot 官网学习笔记 - logging

    commons-logging和slf4j是java中的日志门面,即它们提供了一套通用的接口,具体的实现可以由开发者自由选择.log4j和logback则是具体的日志实现方案. 比较常用的搭配是com ...

  2. C语言I博客作业02

    这个作业属于那个课程  C语言程序设计I 这个作业要求在哪 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/8656 我在这个课程的目标 ...

  3. kotlin -- 可见性修饰符

    puiblic Kotlin的可见修饰符与Java类似,但是默认可见性不同,Java默认包私有,kotlin默认public ### internal internal 只在模块内部可见.一个模块就是 ...

  4. 【译】微软的Python入门教程(一)

    Getting started with Python(Python入门) Overview 概述 The series of videos on Channel 9 is designed to h ...

  5. ELK 学习笔记之 Logstash之codec配置

    Logstash之codec: Logstash处理流程: input=>decode=>filter=>encode=>output 分类: Plain编码: input{ ...

  6. ELK 学习笔记之 Logstash之inputs配置

    Logstash之inputs配置: input plugin doc: https://www.elastic.co/guide/en/logstash/current/index.html 插件很 ...

  7. MongoDB 学习笔记之 检测存储引擎

    检测存储引擎: db.serverStatus().storageEngine db.serverStatus().wiredTiger (转)WiredTiger测试结果 单纯写的测试结果 结论:W ...

  8. 用CSS绘制实体三角形并说明原理

    ;;margin:0 auto;border:6px solid transparent;border-top: 6px solid red;} 1.采用的是均分原理 盒子都是一个矩形或正方形,从形状 ...

  9. python编程基础之二十七

    列表生成式:[exp for iter_var in iterable] 同样也会有字典生成式,集合生成式,没有元组生成式,元组生成式的语法被占用了 字典生成式,集合生成式,就是外面那个括号换成{}  ...

  10. 【TencentOS tiny】深度源码分析(4)——消息队列

    消息队列 在前一篇文章中[TencentOS tiny学习]源码分析(3)--队列 我们描述了TencentOS tiny的队列实现,同时也点出了TencentOS tiny的队列是依赖于消息队列的, ...