package com.qiwu.inference.common.utils; import lombok.Data; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Component; import org.yaml.snakeyaml.Yaml; import java.io.FileInputStream; import java.io.InputStream; import java.util.*; @Component @Data public class YamlUtil { //获取话术存放的路径 public static String speechFilePath = YamlUtil.getValueByKey("speechFilePath"); private static LinkedHashMap>> properties = new LinkedHashMap<>(); /** * 将话术转成map格式 * @param * @return Map>> */ public static Map>> loadSpeechToMap(String fileName) { Map>> speechMap = new HashMap<>(); try { InputStream in = new FileInputStream(speechFilePath + fileName); Yaml yaml = new Yaml(); speechMap = yaml.load(in); } catch (Exception e) { e.printStackTrace(); } return speechMap; } /** * 读取配置文件yml中的某个值,只支持取第一层的值 * @param * @return Map>> */ public static String getValueByKey(String key) { String value = ""; try { Yaml yaml = new Yaml(); ClassPathResource resource = new ClassPathResource("\\application.yml"); properties = yaml.load(resource.getInputStream()); String val = properties.get("spring").get("profiles").get("active"); ClassPathResource resourceNext = new ClassPathResource("\\application-" + val + ".yml"); Map map = yaml.load(resourceNext.getInputStream()); value = String.valueOf(map.get(key)); } catch (Exception e) { e.printStackTrace(); } return value; } }