使用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)文章
springbooot整合dynamic?datasource數(shù)據(jù)庫密碼加密方式
這篇文章主要介紹了springbooot整合dynamic?datasource?數(shù)據(jù)庫密碼加密方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
idea配置檢查XML中SQL語法及書寫sql語句智能提示的方法
idea連接了數(shù)據(jù)庫,也可以執(zhí)行SQL查到數(shù)據(jù),但是無法識別sql語句中的表導(dǎo)致沒有提示,下面這篇文章主要給大家介紹了關(guān)于idea配置檢查XML中SQL語法及書寫sql語句智能提示的相關(guān)資料,需要的朋友可以參考下2023-03-03
SpringBoot使用Graylog日志收集的實現(xiàn)示例
Graylog是一個生產(chǎn)級別的日志收集系統(tǒng),集成Mongo和Elasticsearch進行日志收集,這篇文章主要介紹了SpringBoot使用Graylog日志收集的實現(xiàn)示例,感興趣的小伙伴們可以參考一下2019-04-04
Java實戰(zhàn)房屋租賃網(wǎng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個房屋租賃網(wǎng)站,大家可以在過程中查缺補漏,提升水平2021-11-11

