一。先附一個yml文件的解析步驟
1.Maven依賴
<dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.10</version> </dependency>
2.yml文件
name: hha
age: 60
friend:
- good
- easy
- bug
params:
addr: ZZ
code: EE
name: 洗洗
3.實體類
package com.my.last; import java.util.List; import java.util.Map; public class Student { private String name; private int age; private List<String> friend ; private Map<String, Object> params; /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the friend */ public List<String> getFriend() { return friend; } /** * @param friend the friend to set */ public void setFriend(List<String> friend) { this.friend = friend; } /** * @return the params */ public Map<String, Object> getParams() { return params; } /** * @param params the params to set */ public void setParams(Map<String, Object> params) { this.params = params; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", friend=" + friend + ", params=" + params + "]"; } }
4.測試類
package com.my.last; import org.yaml.snakeyaml.Yaml; public class Test { public static void main(String[] args) { Yaml yaml = new Yaml(); Student student = yaml.loadAs(Test.class.getResourceAsStream("/test.yml"), Student.class); System.out.println(student); }
}
或者依賴
<dependency>
<groupId>org.jyaml</groupId>
<artifactId>jyaml</artifactId>
<version>1.3</version>
</dependency>
解析方法:
Student student2 = null; try { student2 = org.ho.yaml.Yaml.loadType(Test.class.getResourceAsStream("/test.yml"), Student.class); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(student2);
二.二者區別
在於其擁有天然的樹狀結構,所以着手嘗試將properties文件更改為yml文件,發現了幾個要注意的地方:
1、在properties文件中是以”.”進行分割的, 在yml中是用”:”進行分割;
2、yml的數據格式和json的格式很像,都是K-V格式,並且通過”:”進行賦值;
3、在yml中縮進一定不能使用TAB,否則會報很奇怪的錯誤;(縮進特么只能用空格!!!!)
4、yml每個k的冒號后面一定都要加一個空格;
5、使用spring cloud的maven進行構造的項目,在把properties換成yml后,一定要進行mvn clean insatll
6、yml是跨語言的:可以在包括JAVA,go,python等大量的語言中使用,比如做雲計算使用go和java的時候,可以通過配置中心使用同一份配置!
7、支持列表:區別於properties只支持鍵值對數據,yml配置文件支持列表,如下所示:

當然,從properties轉yml文件會遇到很多坑,在此記錄下:
1,層級關系縮進不能用tab鍵:每次都數2.4.6這樣打空格。。。
2,每個key的后面需要加:,每個:后面還需要加一個空格!
3,列表的短橫線后面需要有個空格。
兩個關鍵點:
第一個是yml是支持中文內容的,properties想使用中文只能用unicode編碼
第二個是順序問題,properties是不保證加載順序的,yml有先后順序,實際用例比如springcloud的zuul網關路由配置,如果一個uri同時滿足兩個匹配規則,properties你是不知道它到底使用了哪個規則的,而yml則一定是使用了靠后的那個路由規則
Java 的 Properties 加載屬性文件后是無法保證輸出的順序與文件中一致的,因為 Properties 是繼承自 Hashtable , key/value 都是直接存在 Hashtable 中的,而 Hashtable 是不保證進出順序的。
總有時候會有關心順序一致的需求,恰如有 org.apache.commons.collections.OrderdMap(其實用 LinkedHashMap 就是保證順序) 一樣,我們也想要有個 OrderdProperties。
詳見: https://blog.csdn.net/qq1169091731/article/details/53012071
三。Spring Boot中application.properties和application.yml加載順序
使用@PropertySource注解加載自定義配置文件,該注解無法加載yml配置文件。使用@Value注解獲得文件中的參數值
application.properties和application.yml文件可以放在一下四個位置:
- 外置,在相對於應用程序運行目錄的/congfig子目錄里。
- 外置,在應用程序運行的目錄里
- 內置,在config包內
- 內置,在Classpath根目錄
同樣,這個列表按照優先級排序,也就是說,src/main/resources/config下application.properties覆蓋src/main/resources下application.properties中相同的屬性,如圖:

此外,如果你在相同優先級位置同時有application.properties和application.yml,那么application.properties里面的屬性就會覆蓋里application.yml的屬性
詳見:https://www.cnblogs.com/lukelook/p/10583003.html
