使用Spring從YAML文件讀取內(nèi)容映射為Map方式
從YAML文件讀取內(nèi)容映射為Map
如何在Spring Boot中從YAML文件注入到Map。
首先,將對(duì)Spring框架中的YAML文件有一些了解。 然后,通過(guò)一個(gè)實(shí)際示例展示如何將YAML屬性綁定到Map。
Spring框架中的YAML文件
使用YAML文件存儲(chǔ)外部配置數(shù)據(jù)是一種常見(jiàn)的做法。 基本上,Spring支持使用YAML文檔作為屬性的替代方法,并在后臺(tái)使用SnakeYAML對(duì)其進(jìn)行解析。
看看典型的YAML文件是什么樣的:
server: ? port: 8090 ? application: ? ? name: myapplication ? ? url: http://myapplication.com
從上面可以看出,YAML文件是不言自明的,更易于閱讀。 實(shí)際上,YAML提供了一種簡(jiǎn)潔的方式來(lái)存儲(chǔ)分層配置數(shù)據(jù)。
默認(rèn)情況下,Spring Boot在應(yīng)用程序啟動(dòng)時(shí)從application.properties或application.yml讀取配置屬性。 但是,我們可以使用@PropertySource加載自定義的YAML文件。
既然熟悉了什么是YAML文件,看看如何在Spring Boot中將YAML屬性作為Map注入。
從YAML文件內(nèi)容注入Map
Spring Boot通過(guò)提供一個(gè)方便的注解@ConfigurationProperties,將數(shù)據(jù)的外部化提升到了一個(gè)新的水平。 引入此注解是為了輕松地將配置文件中的外部屬性直接注入Java對(duì)象。
接下來(lái)將介紹如何使用@ConfigurationProperties注解將YAML屬性綁定到bean類中。
首先,在application.yml中定義一些鍵值屬性:
server: ? application: ? ? name: InjectMapFromYAML ? ? url: http://injectmapfromyaml.dev ? ? description: How To Inject a map from a YAML File in Spring Boot ? config: ? ? ips: ? ? ? - 10.10.10.10 ? ? ? - 10.10.10.11 ? ? ? - 10.10.10.12 ? ? ? - 10.10.10.13 ? ? filesystem: ? ? ? - /dev/root ? ? ? - /dev/md2 ? ? ? - /dev/md4 ? users:? ? ? root: ? ? ? username: root ? ? ? password: rootpass ? ? guest: ? ? ? username: guest ? ? ? password: guestpass
其次,創(chuàng)建一個(gè)bean類MapServerProperties來(lái)封裝將配置屬性綁定到Maps的邏輯:
@Configuration @ConfigurationProperties(prefix = "server") @Data public class MapServerProperties { ? ? private Map<String, String> application; ? ? private Map<String, List<String>> config; ? ? private Map<String, Credential> users; ? ? @Data ? ? public static class Credential { ? ? ? ? private String username; ? ? ? ? private String password; ? ? } }
如上面所見(jiàn),我們用@ConfigurationProperties裝飾了MapServerProperties類。 這樣,告訴Spring將具有指定前綴的所有屬性映射到MapServerProperties的對(duì)象。
測(cè)試
@RunWith(SpringRunner.class) @SpringBootTest public class MapFromYamlIntegrationTest { ? ? @Autowired ? ? private MapServerProperties mapServerProperties; ? ? @Test ? ? public void whenYamlFileProvidedThenInjectSimpleMap() { ? ? ? ? assertThat(mapServerProperties.getApplication()) ? ? ? ? ? ? ? ? .containsOnlyKeys("name", "url", "description"); ? ? ? ? assertThat(mapServerProperties.getApplication() ? ? ? ? ? ? ? ? .get("name")).isEqualTo("InjectMapFromYAML"); ? ? } ? ? @Test ? ? public void whenYamlFileProvidedThenInjectComplexMap() { ? ? ? ? assertThat(mapServerProperties.getConfig()).hasSize(2); ? ? ? ? assertThat(mapServerProperties.getConfig() ? ? ? ? ? ? ? ? .get("ips") ? ? ? ? ? ? ? ? .get(0)).isEqualTo("10.10.10.10"); ? ? ? ? assertThat(mapServerProperties.getUsers() ? ? ? ? ? ? ? ? .get("root") ? ? ? ? ? ? ? ? .getUsername()).isEqualTo("root"); ? ? } }
@ConfigurationProperties與@Value
快速比較@ConfigurationProperties和@Value。
盡管兩個(gè)注解均可用于從配置文件注入屬性,但它們卻大不相同。 這兩個(gè)注釋之間的主要區(qū)別在于,每個(gè)注釋具有不同的用途。
簡(jiǎn)而言之,@Value允許我們通過(guò)鍵直接注入特定的屬性值。 但是,@ConfigurationProperties批注將多個(gè)屬性綁定到特定對(duì)象,并提供通過(guò)映射對(duì)象對(duì)屬性的訪問(wèn)。
通常,在注入配置數(shù)據(jù)時(shí),Spring建議在@Value上使用@ConfigurationProperties。 @ConfigurationProperties提供了一種在結(jié)構(gòu)化對(duì)象中集中和分組配置屬性的好方法,以后我們可以將其注入其他bean。
配置文件yml中的map形式
yml中的格式
tes: maps: {key1: 12,key2: 34}
或者
tes: maps: key1: 15 key2: 2
創(chuàng)建一個(gè)類
然后創(chuàng)建對(duì)應(yīng)類型的字段(注意下這個(gè)類的那兩個(gè)注釋了的注解)
package com.etc.lzg; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import java.util.Map; @Data @Component //@Configuration //這個(gè)我這里雖然存在時(shí)能成功,不過(guò)我注釋了也是可以的,這個(gè)是看網(wǎng)上有人寫(xiě)就跟著寫(xiě)上的 //@PropertySource(value = {"classpath:/application.yml"}, encoding = "utf-8") //有的人是寫(xiě)了這個(gè)注解能成功,但是我這邊不能有這個(gè)注解,有的話,就連編譯都會(huì)報(bào)錯(cuò) @ConfigurationProperties(prefix = "tes") public class MapTest { private Map<String, String> maps; }
引用
package com.etc.lzg; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class LzgApplicationTests { @Autowired private MapTest mapTest; @Test public void contextLoads() { System.out.println(mapTest.toString()); System.out.println("key1="+mapTest.getMaps().get("key1")); } }
打印
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA全局內(nèi)容搜索和替換教程圖解
很多朋友在做項(xiàng)目時(shí),會(huì)在整個(gè)項(xiàng)目里活指定文件夾下進(jìn)行全局搜索和替換,下面小編給大家?guī)?lái)了IntelliJ IDEA全局內(nèi)容搜索和替換教程圖解,需要的朋友參考下吧2018-04-04spring boot創(chuàng)建項(xiàng)目包依賴問(wèn)題的解決
本篇文章主要介紹了spring boot創(chuàng)建項(xiàng)目包依賴問(wèn)題的解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11淺談Servlet的Cookie和Session機(jī)制
雖然session機(jī)制在web應(yīng)用程序中被采用已經(jīng)很長(zhǎng)時(shí)間了,但是仍然有很多人不清楚session機(jī)制的本質(zhì),以至不能正確的應(yīng)用這一技術(shù).本文將詳細(xì)討論session以及cookie的工作機(jī)制,需要的朋友可以參考下2021-05-05IDEA入門(mén)級(jí)使用教程你居然還在用eclipse?
上個(gè)月,idea的使用量超越eclipse的消息席卷了整個(gè)IT界,idea到底好在哪里呢?下面小編通過(guò)本文給大家詳細(xì)介紹下IDEA入門(mén)級(jí)使用教程,非常詳細(xì),感興趣的朋友一起看看吧2020-10-10關(guān)于springboot集成swagger及knife4j的增強(qiáng)問(wèn)題
這篇文章主要介紹了springboot集成swagger以及knife4j的增強(qiáng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03