溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么將YAML中的列表映射到Java List中

發(fā)布時間:2021-11-30 14:27:55 來源:億速云 閱讀:939 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要講解了“怎么將YAML中的列表映射到Java List中”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么將YAML中的列表映射到Java List中”吧!

1. 概述

在這個簡短的教程中,我們將進一步了解如何在Spring Boot中將YAML列表映射到列表中。

我們首先介紹一些如何在YAML中定義列表的背景知識。然后,我們將深入研究如何將YAML列表綁定到對象列表。 

2. 快速回顧一下YAML中的列表

簡而言之,YAML是一種人類可讀的數(shù)據(jù)序列化標(biāo)準,它提供了一種簡潔而清晰的方式來編寫配置文件。YAML的優(yōu)點是它支持多種數(shù)據(jù)類型,如列表、映射和標(biāo)量類型。

YAML列表中的元素使用“-”字符定義,它們共享相同的縮進級別:

yamlconfig:
 list:
   - item1
   - item2
   - item3
   - item4

 

與properties對比:

yamlconfig.list[0]=item1
yamlconfig.list[1]=item2
yamlconfig.list[2]=item3
yamlconfig.list[3]=item4
 

事實上,與屬性文件相比,YAML的層次性顯著增強了可讀性。YAML的另一個有趣的特性是可以為不同的Spring配置文件定義不同的屬性。

值得一提的是,Spring引導(dǎo)為YAML配置提供了開箱即用的支持。按照設(shè)計,Spring引導(dǎo)從應(yīng)用程序加載配置屬性。yml啟動,沒有任何額外的工作。 

3.將一個YAML列表綁定到一個簡單的對象列表

Spring Boot提供了@ConfigurationProperties注釋來簡化將外部配置數(shù)據(jù)映射到對象模型的邏輯。

在本節(jié)中,我們將使用@ConfigurationProperties將一個YAML列表綁定到list中。


我們首先在application.yml中定義一個簡單的列表:

application:
 profiles:
   - dev
   - test
   - prod
   - 1
   - 2
 

然后,我們將創(chuàng)建一個簡單的ApplicationProps POJO來保存將YAML列表綁定到對象列表的邏輯:

@Component
@ConfigurationProperties(prefix = "application")
public class ApplicationProps {

   private List<Object> profiles;

   // getter and setter

}
 

ApplicationProps類需要用@ConfigurationProperties進行裝飾,以表達將所有帶有指定前綴的YAML屬性映射到ApplicationProps對象的意圖。

要綁定profiles列表,我們只需要定義一個list類型的字段,其余的由@ConfigurationProperties注釋處理。

注意,我們使用@Component將ApplicationProps類注冊為一個普通的Spring bean。因此,我們可以以與任何其他Spring bean相同的方式將其注入到其他類中。

最后,我們將ApplicationProps bean注入到一個測試類中,并驗證我們的概要文件YAML列表是否被正確注入為list:


@ExtendWith(SpringExtension.class)
@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
@EnableConfigurationProperties(value = ApplicationProps.class)
class YamlSimpleListUnitTest {

   @Autowired
   private ApplicationProps applicationProps;

   @Test
   public void whenYamlList_thenLoadSimpleList() {
       assertThat(applicationProps.getProfiles().get(0)).isEqualTo("dev");
       assertThat(applicationProps.getProfiles().get(4).getClass()).isEqualTo(Integer.class);
       assertThat(applicationProps.getProfiles().size()).isEqualTo(5);
   }
   

4. 將YAML列表綁定到復(fù)雜列表

現(xiàn)在,讓我們進一步了解如何將嵌套的YAML列表注入到復(fù)雜的結(jié)構(gòu)化列表中。

首先,讓我們添加一些嵌套列表到application.yml:

application:
 // ...
 props:
   -
     name: YamlList
     url: http://yamllist.dev
     description: Mapping list in Yaml to list of objects in Spring Boot
   -
     ip: 10.10.10.10
     port: 8091
   -
     email: support@yamllist.dev
     contact: http://yamllist.dev/contact
 users:
   -
     username: admin
     password: admin@10@
     roles:
       - READ
       - WRITE
       - VIEW
       - DELETE
   -
     username: guest
     password: guest@01
     roles:
       - VIEW
 

在這個例子中,我們將道具屬性綁定到一個 List<Map<String, Object>>.。類似地,我們將把用戶映射到User對象列表中。

但是,在用戶的情況下,所有的項共享相同的鍵,所以為了簡化它的映射,我們可能需要創(chuàng)建一個專用的用戶類,將鍵封裝為字段:

public class ApplicationProps {

   // ...

   private List<Map<String, Object>> props;
   private List<User> users;

   // getters and setters

   public static class User {

       private String username;
       private String password;
       private List<String> roles;

       // getters and setters

   }
}
 

現(xiàn)在我們驗證嵌套的YAML列表被正確映射:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
@EnableConfigurationProperties(value = ApplicationProps.class)
class YamlComplexListsUnitTest {

   @Autowired
   private ApplicationProps applicationProps;

   @Test
   public void whenYamlNestedLists_thenLoadComplexLists() {
       assertThat(applicationProps.getUsers().get(0).getPassword()).isEqualTo("admin@10@");
       assertThat(applicationProps.getProps().get(0).get("name")).isEqualTo("YamlList");
       assertThat(applicationProps.getProps().get(1).get("port").getClass()).isEqualTo(Integer.class);
   }

}

感謝各位的閱讀,以上就是“怎么將YAML中的列表映射到Java List中”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么將YAML中的列表映射到Java List中這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI