使用springboot配置文件yml中的map形式
更新時間:2021年08月18日 15:24:20 作者:MTone1
這篇文章主要介紹了springboot配置文件yml中的map形式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
springboot配置文件yml的map形式
1、yml中的格式
tes: maps: {key1: 12,key2: 34}
或者
tes: maps: key1: 15 key2: 2
2、創(chuàng)建一個類
然后創(chuàng)建對應(yīng)類型的字段(注意下這個類的那兩個注釋了的注解)
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 //這個我這里雖然存在時能成功,不過我注釋了也是可以的,這個是看網(wǎng)上有人寫就跟著寫上的 //@PropertySource(value = {"classpath:/application.yml"}, encoding = "utf-8") //有的人是寫了這個注解能成功,但是我這邊不能有這個注解,有的話,就連編譯都會報錯 @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
配置文件對應(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 方法 。。。。 */ } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot之idea之pom文件圖標(biāo)不對問題
這篇文章主要介紹了Springboot之idea之pom文件圖標(biāo)不對問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04Java Math類、Random類、System類及BigDecimal類用法示例
這篇文章主要介紹了Java Math類、Random類、System類及BigDecimal類用法,結(jié)合實例形式分析了java數(shù)值運算相關(guān)的Math類、Random類、System類及BigDecimal類基本功能與使用技巧,需要的朋友可以參考下2019-03-03springboot-rabbitmq-reply?消息直接回復(fù)模式詳情
這篇文章主要介紹了springboot-rabbitmq-reply消息直接回復(fù)模式詳情,文章通過圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-09-09Springboot工具類ReflectionUtils使用教程
這篇文章主要介紹了Springboot內(nèi)置的工具類之ReflectionUtils的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12詳解在springboot中使用Mybatis Generator的兩種方式
這篇文章主要介紹了詳解在springboot中使用Mybatis Generator的兩種方式,本文將介紹到在springboot的項目中如何去配置和使用MBG以及MBG生成代碼的兩種方式,非常具有實用價值,需要的朋友可以參考下2018-11-11