1.
/** * 读取bootstrap配置文件 */ @Component public class YmlUtils { private static final Map<String, String> confMap = new HashMap(256); static { ClassLoader classLoader = YmlUtils.class.getClassLoader(); Yaml yaml = new Yaml(); InputStream bootstrapIn = classLoader.getResourceAsStream("bootstrap.yml"); if (bootstrapIn != null) { fillConfMap(yaml.loadAs(bootstrapIn, Map.class), confMap, null); IOUtils.closeQuietly(bootstrapIn); } } private static void fillConfMap(Map<String, Object> sourceMap, Map<String, String> destinationMap, String key) { sourceMap.forEach((key1, v) -> { String k = key != null ? key + "." + key1 : key1; if (v instanceof Map) { fillConfMap((Map) v, destinationMap, k); } else { destinationMap.put(k, String.valueOf(v)); } }); } public static String getConfig(String key, String defualtValue) { String value = confMap.get(key); return value != null && !"".equals(value.trim()) ? value : defualtValue; } public static String getConfig(String key) { return confMap.get(key); } public static String getAppName(){ return getConfig("spring.application.name"); } }