Spring Boot中优雅的获取yml文件工具类
如何在spring boot
中优雅的获取.yml
文件工具类呢
代码如下:
package com.common.base.utils.base; import com.common.base.generator.ResourceManager; import org.yaml.snakeyaml.Yaml; import java.io.InputStream; import java.util.HashMap; import java.util.Map; /** * yml文件工具类 */ public class YmlUtils { private static String bootstrap_file = "bootstrap.yml"; private static Map<String,String> result = new HashMap<>(); /** * 根据文件名获取yml的文件内容 * @return */ public static Map<String,String> getYmlByFileName(String file){ result = new HashMap<>(); if(file == null) file = bootstrap_file; InputStream in = ResourceManager.class.getClassLoader().getResourceAsStream(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(val instanceof Map){ forEachYaml(key,(Map<String, Object>) val); }else{ result.put(key,val.toString()); } } return result; } /** * 根据key获取值 * @param key * @return */ public static String getValue(String key){ Map<String,String> map = getYmlByFileName(null); if(map==null)return null; return map.get(key); } /** * 遍历yml文件,获取map集合 * @param key_str * @param obj * @return */ public static Map<String,String> forEachYaml(String key_str,Map<String, Object> obj){ for(Map.Entry<String,Object> entry:obj.entrySet()){ String key = entry.getKey(); Object val = entry.getValue(); String str_new = ""; if(StringUtils.isNotNull(key_str)){ str_new = key_str+ "."+key; }else{ str_new = key; } if(val instanceof Map){ forEachYaml(str_new,(Map<String, Object>) val); }else{ result.put(str_new,val.toString()); } } return result; } /** * 获取bootstrap.yml的name * @return */ public static String getApplicationName(){ return getYmlByFileName(bootstrap_file).get("spring.application.name"); } /** * 获取bootstrap.yml的name * @return */ public static String getApplicationName1(){ String name = getYmlByFileName(bootstrap_file).get("spring.application.name"); return name + "center"; } public static void main(String[] args) { System.out.println(getApplicationName()); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接
赞 (0)