一 使用 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 加載為Properties
,YamlMapFactoryBean
將 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
。如果未啟用development
,production
和eu-central
profiles,則 property 的 value 為192.168.1.100
。
因此,
spring.profiles
可以包含一個簡單的 profile name(用於 exampleproduction
)或 profile 表達式。對於 exampleproduction & (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
屬性和值也是大小寫敏感;
字面量:普通的值(數字,字符串,布爾)
k: v:字面直接來寫;
字符串默認不用加上單引號或者雙引號;
"":雙引號;不會轉義字符串里面的特殊字符;特殊字符會作為本身想表示的意思
name: "zhangsan \n lisi":輸出;zhangsan 換行 lisi
'':單引號;會轉義特殊字符,特殊字符最終只是一個普通的字符串數據
name: ‘zhangsan \n lisi’:輸出;zhangsan \n lisi
對象、Map(屬性和值)(鍵值對):
k: v:在下一行來寫對象的屬性和值的關系;注意縮進
對象還是k: v的方式
friends
行內寫法:
friends
數組(List、Set):
用- 值表示數組中的一個元素
pets
行內寫法
pets