使用springboot配置文件yml中的map形式
springboot配置文件yml的map形式
1、yml中的格式
tes: maps: {key1: 12,key2: 34}
或者
tes: maps: key1: 15 key2: 2
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)上有人寫就跟著寫上的 //@PropertySource(value = {"classpath:/application.yml"}, encoding = "utf-8") //有的人是寫了這個(gè)注解能成功,但是我這邊不能有這個(gè)注解,有的話,就連編譯都會(huì)報(bào)錯(cuò) @ConfigurationProperties(prefix = "tes") public class MapTest { private Map<String, String> maps; }
3、引用
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")); } }
4、打印
SpringBoot yaml文件map集合使用
yaml文件配置
patform.config: maps: person_one: userName: A platform: A platform person_two: userName: B platform: B platform
配置文件對(duì)應(yīng)的bean
如果yaml文件不是在application.yaml,則注解需要配置locations屬性
@ConfigurationProperties(value="platform.config",locations="classpath:config/applicaion-platform.yaml") public class ParamConfiguration{ private Map<String,ParamInfo> maps =new LinkedHashMap<String,ParamInfo>(); /** set ,get 方法 。。。。 */ public static class ParamInfo{ private String username; private String platform; /** set ,get 方法 。。。。 */ } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
普通對(duì)象使用spring容器中的對(duì)象的實(shí)現(xiàn)方法
這篇文章主要介紹了普通對(duì)象使用spring容器中的對(duì)象的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Springboot之idea之pom文件圖標(biāo)不對(duì)問(wèn)題
這篇文章主要介紹了Springboot之idea之pom文件圖標(biāo)不對(duì)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Java Math類、Random類、System類及BigDecimal類用法示例
這篇文章主要介紹了Java Math類、Random類、System類及BigDecimal類用法,結(jié)合實(shí)例形式分析了java數(shù)值運(yùn)算相關(guān)的Math類、Random類、System類及BigDecimal類基本功能與使用技巧,需要的朋友可以參考下2019-03-03springboot-rabbitmq-reply?消息直接回復(fù)模式詳情
這篇文章主要介紹了springboot-rabbitmq-reply消息直接回復(fù)模式詳情,文章通過(guò)圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-09-09Springboot工具類ReflectionUtils使用教程
這篇文章主要介紹了Springboot內(nèi)置的工具類之ReflectionUtils的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12詳解在springboot中使用Mybatis Generator的兩種方式
這篇文章主要介紹了詳解在springboot中使用Mybatis Generator的兩種方式,本文將介紹到在springboot的項(xiàng)目中如何去配置和使用MBG以及MBG生成代碼的兩種方式,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-11-11