Springboot?yml?Map?List讀取方式
Springboot yml Map List讀取
Springboot 讀取yml中的值作為參數(shù),有2種寫(xiě)法:
例子:
已知有如下yml配置:
? myapps: ? ? 1131f78c313e11e79da3000c298bdf0e: AMS系統(tǒng) ? ? 1ae211c543a14cf6981274fec3281f0c: BMS系統(tǒng) ? ? app1: 測(cè)試 ??
現(xiàn)在需要將myapps讀到配置文件作為配置類。有2種做法:
1、配置類集中放置
也就是說(shuō)應(yīng)用中設(shè)一個(gè)配置類,所有涉及yml配置的類都放置在該類中。
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class MyConfiguration {
?? ?static Logger log = LoggerFactory.getLogger(MyConfiguration.class);?? ?
?? ?
?? ?@Bean
?? ?public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
?? ? ? return new PropertySourcesPlaceholderConfigurer();
?? ?}?? ??
?? ??
?? ?@Value("${px.a.trackerServer}")
?? ?String trackerServer = "";??? ?
?
?? ?@Value("${px.a.disabled:true}")
?? ?boolean disableA = true;?? ?
?? ?
?? ?@Bean
?? ?@ConfigurationProperties(prefix = "px.client")
?? ?ClientConfigInfo clientConfigInfo(){
?? ??? ?ClientConfigInfo clientConfigInfo = new ClientConfigInfo();?? ??? ?
?? ??? ?return clientConfigInfo;
?? ?}?? ?
?? ?
?? ?@Bean
?? ?@ConfigurationProperties(prefix = "px")
?? ? ? ? ? ?MyAppConfiguration myAppConfiguration(){
?? ??? ?MyAppConfiguration myAppConfiguration = new MyAppConfiguration ();
?? ??? ?return myAppConfiguration;
?? ?}??
}這里配置為:
? ? @Bean
? ? @ConfigurationProperties(prefix = "px")
? ? UdsAppConfiguration udsAppConfiguration(){undefined
? ? ? ? UdsAppConfiguration udsAppConfiguration = new UdsAppConfiguration();
? ? ? ? return udsAppConfiguration;
? ? }2、配置類單獨(dú)放置
也就是說(shuō)把MyAppConfiguration單獨(dú)作為一個(gè)配置類進(jìn)行單獨(dú)的組裝。
@Component ?
@ConfigurationProperties(prefix="px", ignoreInvalidFields=true, ignoreUnknownFields=true)
public class MyAppConfiguration {
?? ?static Logger log = LoggerFactory.getLogger(MyAppConfiguration.class);??? ?
?? ?private Map<String, String> mysapps = new HashMap<String, String>();??
?? ?public Map<String, String> getMyapps() {
?? ??? ?return myapps;
?? ?}??
?? ?public void setMyapps(Map<String, String> myapps) {
?? ??? ?this.myapps = myapps;
?? ?}??
}3、List讀取
(1)yml
? myapps: ? ? - 1131f78c313e11e79da3000c298bdf0e: AMS系統(tǒng) ? ? - 1ae211c543a14cf6981274fec3281f0c: BMS系統(tǒng) ? ? - app1: 測(cè)試 ?
(2)配置類寫(xiě)法
@Component ?
@ConfigurationProperties(prefix="px", ignoreInvalidFields=true, ignoreUnknownFields=true)
public class MyAppConfiguration {
? ? static Logger log = LoggerFactory.getLogger(MyAppConfiguration.class);? ??
? ? private List<Map<String, String>> mysapps = new ArrayList<Map<String, String>>();??
? ? public List<Map<String, String>> getMyapps() {
? ? ? ? return myapps;
? ? }?
? ? public void setMyapps(List<Map<String, String>> myapps) {
? ? ? ? this.myapps = myapps;
? ? }?
}4、問(wèn)題:配置類沒(méi)有獲取到值
該問(wèn)題的原因很可能是yml中定義的key與配置類中定義的屬性名稱不一致,導(dǎo)致Springboot自動(dòng)裝配時(shí)失敗。出現(xiàn)該問(wèn)題后,一定首先要仔細(xì)檢查配置類對(duì)應(yīng)屬性的名稱以及配置類映射的路徑。
Springboot yml內(nèi)list、map組合寫(xiě)法
yml:
myProps:
? varmaplist:
? ? ? key11:
? ? ? ? - t1
? ? ? ? - t2
? ? ? ? - t3
? ? ? key22:
? ? ? ? - t11
? ? ? ? - t22
? ? ? ? - t33
? list:
? ? - topic1
? ? - topic2
? ? - topic3
? maps: {key1: 'value1', key2: 'value2'}MyProps:
@Component
@Data
@Configuration
@PropertySource(value = {"classpath:/bootstrap.yml"}, encoding = "utf-8")
@ConfigurationProperties(prefix = "myProps")
public class MyProps {
? ? private List<String> list;
? ? private Map<String,String> maps;
? ? private Map<String,List<String>> varmaplist;
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Maven構(gòu)建Hadoop項(xiàng)目的實(shí)踐步驟
本文主要介紹了Maven構(gòu)建Hadoop項(xiàng)目的實(shí)踐步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
SpringBoot3整合Swagger3時(shí)出現(xiàn)Type javax.servlet.http.H的ttpSe
這篇文章主要介紹了SpringBoot3整合Swagger3時(shí)出現(xiàn)Type javax.servlet.http.H的ttpServletRequest not present錯(cuò)誤解決方法,文中有詳細(xì)的解決方法,需要的朋友可以參考下2025-01-01
java IO實(shí)現(xiàn)電腦搜索、刪除功能的實(shí)例
下面小編就為大家?guī)?lái)一篇java IO實(shí)現(xiàn)電腦搜索、刪除功能的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
IntelliJ IDEA中如何調(diào)試Java Stream操作
這篇文章主要介紹了IntelliJ IDEA中如何優(yōu)雅的調(diào)試Java Stream操作,在強(qiáng)大的IDEA插件支持下,stream的調(diào)試其實(shí)也沒(méi)那么難了,下面就來(lái)學(xué)習(xí)一下在IDEA中如何調(diào)試stream操作吧2022-05-05
Java使用BIO和NIO進(jìn)行文件操作對(duì)比代碼示例
這篇文章主要介紹了Java使用BIO和NIO進(jìn)行文件操作對(duì)比代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
IDEA入門(mén)級(jí)使用教程你居然還在用eclipse?
上個(gè)月,idea的使用量超越eclipse的消息席卷了整個(gè)IT界,idea到底好在哪里呢?下面小編通過(guò)本文給大家詳細(xì)介紹下IDEA入門(mén)級(jí)使用教程,非常詳細(xì),感興趣的朋友一起看看吧2020-10-10
SpringBoot項(xiàng)目修改訪問(wèn)端口和訪問(wèn)路徑的方法
這篇文章主要介紹了SpringBoot項(xiàng)目修改訪問(wèn)端口和訪問(wèn)路徑的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
PageHelper插件實(shí)現(xiàn)服務(wù)器端分頁(yè)功能
這篇文章主要為大家詳細(xì)介紹了PageHelper插件實(shí)現(xiàn)服務(wù)器端分頁(yè)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07

