package com.hq.schoolcj.util;import lombok.extern.slf4j.Slf4j;import org.yaml.snakeyaml.Yaml;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 获取yml参数配置帮助类 */@Slf4jpublic class YamlUtil { private Pattern p1 = Pattern.compile("\\$\\{.*?\\}"); /** * key:文件索引名 * value:配置文件内容 */ private static Map<String , LinkedHashMap> ymls = new HashMap<>(); /** * String:当前线程需要查询的文件名 */ private static ThreadLocal<String> nowFileName = new InheritableThreadLocal<>(); /** * 加载配置文件 * @param fileName */ public static void loadYml(String fileName){ nowFileName.set(fileName); if (!ymls.containsKey(fileName)){ ymls.put(fileName , new Yaml().loadAs(YamlUtil.class.getResourceAsStream("/" + fileName),LinkedHashMap.class)); } } /** * 读取yml文件中的某个value。 * 支持解析 yml文件中的 ${} 占位符 * @param key * @return Object */ public Object getValue(String key){ String[] keys = key.split("[.]"); Map ymlInfo = (Map) ymls.get(nowFileName.get()).clone(); for (int i = 0; i < keys.length; i++) { Object value = ymlInfo.get(keys[i]); if (i < keys.length - 1){ ymlInfo = (Map) value; }else if (value == null){ throw new RuntimeException("key不存在"); }else { String g; String keyChild; String v1 = (String)value; for(Matcher m = p1.matcher(v1); m.find(); value = v1.replace(g, (String)getValue(keyChild))) { g = m.group(); keyChild = g.replaceAll("\\$\\{", "").replaceAll("\\}", ""); } return value; } } return ""; } /** * 读取yml文件中的某个value * @param fileName yml名称 * @param key * @return Object */ public Object getValue(String fileName , String key){ loadYml(fileName); return getValue(key); } /** * 框架私有方法,非通用。 * 获取 server.servlet.context-path的值 * @return */ public String getContextPath(){ return (String) getValue("application.yml", "server.servlet.context-path"); } /** * 读取yml文件中的某个value,返回String * @param fileName * @param key * @return String */ public String getValueToString(String fileName , String key) { return (String) getValue(fileName, key); }}