如何基于JAVA讀取yml配置文件指定key內(nèi)容
這篇文章主要介紹了如何基于JAVA讀取yml配置文件指定key內(nèi)容,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
先引入需要的依賴
<!--讀取yml文件-->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.23</version>
</dependency>
讀取YML文件工具類的代碼
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import org.yaml.snakeyaml.Yaml;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author hunmeng
* @create 2020-01-10 20:34
*/
public class YmlUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(YmlUtils.class);
private static String bootstrap_file = "classpath:application-test.yml";
private static Map<String,String> result = new HashMap<>();
/**
* 根據(jù)文件名獲取yml的文件內(nèi)容
* @param filePath
* @param keys 第一個(gè)參數(shù)對(duì)應(yīng)第一個(gè)key,第二個(gè)參數(shù)對(duì)應(yīng)第二個(gè)key 比如spring.name下的所有 就是兩個(gè)參數(shù)、
* getYmlByFileName(bootstrap_file,"spring", "name");
* @return
*/
public static Map<String,String> getYmlByFileName(String filePath, String... keys) {
result = new HashMap<>();
if(filePath == null) filePath = bootstrap_file;
InputStream in = null;
try {
File file = ResourceUtils.getFile(filePath);
in = new BufferedInputStream(new FileInputStream(file));
Yaml props = new Yaml();
Object obj = props.loadAs(in,Map.class);
Map<String,Object> param = (Map<String, Object>) obj;
for(Map.Entry<String,Object> entry:param.entrySet()){
String key = entry.getKey();
Object val = entry.getValue();
if (keys.length != 0 && !keys[0].equals(key)){
continue;
}
if(val instanceof Map){
forEachYaml(key,(Map<String, Object>) val, 1, keys);
}else{
result.put(key, val.toString());
}
}
return result;
} catch (FileNotFoundException e) {
LOGGER.error(e.getMessage(),e);
}finally {
if (in != null){
try {
in.close();
} catch (IOException e) {
LOGGER.error(e.getMessage(),e);
}
}
}
return null;
}
/**
* 根據(jù)key獲取值
* @param key
* @return
*/
public static String getValue(String key) throws FileNotFoundException {
Map<String,String> map = getYmlByFileName(null);
if(map==null)return null;
return map.get(key);
}
/**
* 遍歷yml文件,獲取map集合
* @param key_str
* @param obj
* @param i
* @param keys
* @return
*/
public static Map<String,String> forEachYaml(String key_str,Map<String, Object> obj, int i, String... keys){
for(Map.Entry<String,Object> entry:obj.entrySet()){
String key = entry.getKey();
Object val = entry.getValue();
if (keys.length > i && !keys[i].equals(key)){
continue;
}
String str_new = "";
if(StringUtils.isNotEmpty(key_str)){
str_new = key_str+ "."+key;
}else{
str_new = key;
}
if(val instanceof Map){
forEachYaml(str_new,(Map<String, Object>) val, ++i, keys);
i--;
}else{
result.put(str_new,val.toString());
}
}
return result;
}
/**
* 獲取bootstrap.yml的name
* @return
*/
public static String getApplicationName() throws FileNotFoundException {
return getYmlByFileName(bootstrap_file).get("server.port");
}
/**
* 獲取bootstrap.yml的name
* @return
*/
public static String getApplicationName1() throws FileNotFoundException {
String name = getYmlByFileName(bootstrap_file).get("spring.application.name");
return name + "center";
}
public static void main(String[] args) throws FileNotFoundException {
Map<String, String> ymlByFileName = getYmlByFileName(bootstrap_file,"spring");
Set<Map.Entry<String, String>> entries = ymlByFileName.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry.getKey()+"==="+entry.getValue());
}
System.out.println(getApplicationName());
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Base64加解密的實(shí)現(xiàn)方式實(shí)例詳解
這篇文章主要介紹了Base64加解密的實(shí)現(xiàn)方式實(shí)例詳解的相關(guān)資料,這里提供了實(shí)現(xiàn)實(shí)例,幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
Spring使用@Retryable實(shí)現(xiàn)自動(dòng)重試機(jī)制
在微服務(wù)架構(gòu)中,服務(wù)之間的調(diào)用可能會(huì)因?yàn)橐恍簳r(shí)性的錯(cuò)誤而失敗,例如網(wǎng)絡(luò)波動(dòng)、數(shù)據(jù)庫(kù)連接超時(shí)或第三方服務(wù)不可用等,在本文中,我們將介紹如何在 Spring 中使用 @Retryable 實(shí)現(xiàn)自動(dòng)重試機(jī)制,需要的朋友可以參考下2025-01-01
MyBatisPlus 自定義sql語(yǔ)句的實(shí)現(xiàn)
這篇文章主要介紹了MyBatisPlus 自定義sql語(yǔ)句的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
springboot 定時(shí)任務(wù)@Scheduled實(shí)現(xiàn)解析
這篇文章主要介紹了springboot 定時(shí)任務(wù)@Scheduled實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
SpringBoot如何在運(yùn)行時(shí)動(dòng)態(tài)添加數(shù)據(jù)源
這篇文章主要介紹了SpringBoot如何在運(yùn)行時(shí)動(dòng)態(tài)添加數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
完美解決Eclipse 項(xiàng)目有紅感嘆號(hào)的問(wèn)題
下面小編就為大家?guī)?lái)一篇完美解決Eclipse 項(xiàng)目有紅感嘆號(hào)的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01

