YAML格式文件实践

前言

yaml格式现在已经很通用了,yaml有简洁易读的格式,在容器环境或者微服务被大量使用,只是使用的过程还是properties的key的方式存储使用,可能跟Spring的发展历程有关,Spring使用PropertySource存储配置数据,而原生的yaml使用还有很多不便利的地方。

1. yaml yml properties

实际上yaml yml都是一种格式,Spring仅定义了Yaml的类,properties是HashTable包装。

在Spring的存储中是用PropertySource存储内容,所以在Spring boot微服务体系,@Value注入还是Properties的key,只是格式上是yaml文件。

2. 转换与使用

先试试把yaml文件转为properties文件

public class YamlPropertiesTest {

    @Test
    public void testYamlToProperties() {
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(new ClassPathResource("application.yaml"));
        System.out.println(yamlPropertiesFactoryBean.getObject());
    }

    @Test
    public void testYamlPropertySourceLoader() throws IOException {
        YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
        List<PropertySource<?>> propertySourceList = yamlPropertySourceLoader.load("application", new ClassPathResource("application.yaml"));
        System.out.println(propertySourceList.get(0).getSource());
    }



application.yaml

server:
  port: 8080

my:
  def:
    addr: localhost:9876

都是用的Spring boot封装的类,实际上Spring boot也是使用的snakeyaml来处理yaml的,分析解析源码

看Spring boot怎么解析yaml的 org.springframework.beans.factory.config.YamlProcessor

	/**
	 * Provide an opportunity for subclasses to process the Yaml parsed from the supplied
	 * resources. Each resource is parsed in turn and the documents inside checked against
	 * the {@link #setDocumentMatchers(DocumentMatcher...) matchers}. If a document
	 * matches it is passed into the callback, along with its representation as Properties.
	 * Depending on the {@link #setResolutionMethod(ResolutionMethod)} not all of the
	 * documents will be parsed.
	 * @param callback a callback to delegate to once matching documents are found
	 * @see #createYaml()
	 */
	protected void process(MatchCallback callback) {
        //先创建Yaml对象,本质就是map,跟据yaml的格式,每一层都是key,缩进map
		Yaml yaml = createYaml();
		for (Resource resource : this.resources) {//yaml文件
            //process解析yaml,优先解析为缩进map,即原生格式;可转为properties
			boolean found = process(callback, yaml, resource);
			if (this.resolutionMethod == ResolutionMethod.FIRST_FOUND && found) {
				return;
			}
		}
	}

process(callback, yaml, resource);

	private boolean process(MatchCallback callback, Yaml yaml, Resource resource) {
		int count = 0;
		try {
			if (logger.isDebugEnabled()) {
				logger.debug("Loading from YAML: " + resource);
			}
            //UnicodeReader,考虑真周到
			try (Reader reader = new UnicodeReader(resource.getInputStream())) {
                //yaml.loadAll载入就是缩进map了
				for (Object object : yaml.loadAll(reader)) {
                    //asMap解决key是非String的情况
					if (object != null && process(asMap(object), callback)) {
						count++;
						if (this.resolutionMethod == ResolutionMethod.FIRST_FOUND) {
							break;
						}
					}
				}
				if (logger.isDebugEnabled()) {
					logger.debug("Loaded " + count + " document" + (count > 1 ? "s" : "") +
							" from YAML resource: " + resource);
				}
			}
		}
		catch (IOException ex) {
			handleProcessError(resource, ex);
		}
		return (count > 0);
	}

 yaml.loadAll原生缩进map结果

 asMap,会把key不是字符串的转为字符串。

	private Map<String, Object> asMap(Object object) {
		// YAML can have numbers as keys
		Map<String, Object> result = new LinkedHashMap<>();
		if (!(object instanceof Map)) {
			// A document can be a text literal
			result.put("document", object);
			return result;
		}

		Map<Object, Object> map = (Map<Object, Object>) object;
		map.forEach((key, value) -> {
			if (value instanceof Map) {
				value = asMap(value);
			}
			if (key instanceof CharSequence) {
				result.put(key.toString(), value);
			}
			else {
				// It has to be a map key in this case
				result.put("[" + key.toString() + "]", value);
			}
		});
		return result;
	}

继续看看转换逻辑

	private boolean process(Map<String, Object> map, MatchCallback callback) {
		Properties properties = CollectionFactory.createStringAdaptingProperties();
        //这里就把yaml转为properties文件存储了
		properties.putAll(getFlattenedMap(map));

        //有没有专门的匹配器,没有直接callback处理
		if (this.documentMatchers.isEmpty()) {
			if (logger.isDebugEnabled()) {
				logger.debug("Merging document (no matchers set): " + map);
			}
			callback.process(properties, map);
			return true;
		}

        //匹配器处理逻辑
		MatchStatus result = MatchStatus.ABSTAIN;
		for (DocumentMatcher matcher : this.documentMatchers) {
			MatchStatus match = matcher.matches(properties);
			result = MatchStatus.getMostSpecific(match, result);
			if (match == MatchStatus.FOUND) {
				if (logger.isDebugEnabled()) {
					logger.debug("Matched document with document matcher: " + properties);
				}
				callback.process(properties, map);
				return true;
			}
		}

		if (result == MatchStatus.ABSTAIN && this.matchDefault) {
			if (logger.isDebugEnabled()) {
				logger.debug("Matched document with default matcher: " + map);
			}
			callback.process(properties, map);
			return true;
		}

		if (logger.isDebugEnabled()) {
			logger.debug("Unmatched document: " + map);
		}
		return false;
	}

继续跟踪

解析代码,递归解析

	private void buildFlattenedMap(Map<String, Object> result, Map<String, Object> source, @Nullable String path) {
		source.forEach((key, value) -> {
			if (StringUtils.hasText(path)) {
                // 前面asMap处理的
				if (key.startsWith("[")) {
					key = path + key;
				}
				else {
                    //拼接properties的key
					key = path + '.' + key;
				}
			}
			if (value instanceof String) {
				result.put(key, value);
			}
            //缩进map结构,递归map
			else if (value instanceof Map) {
				// Need a compound key
				@SuppressWarnings("unchecked")
				Map<String, Object> map = (Map<String, Object>) value;
				buildFlattenedMap(result, map, key);
			}
            //集合处理
			else if (value instanceof Collection) {
				// Need a compound key
				@SuppressWarnings("unchecked")
				Collection<Object> collection = (Collection<Object>) value;
				if (collection.isEmpty()) {
					result.put(key, "");
				}
				else {
					int count = 0;
                    //把集合的每个元素作为value,key为[下标],form表单处理逻辑😅
					for (Object object : collection) {
						buildFlattenedMap(result, Collections.singletonMap(
								"[" + (count++) + "]", object), key);
					}
				}
			}
			else {
				result.put(key, (value != null ? value : ""));
			}
		});
	}

此致解析结束。

另外yaml可以直接解析map,也可以把json解析为yaml的map,当然也可以解析为对象,本质跟json解析为对象差不多。

    @Test
    public void testYamlToMap(){
        YamlMapFactoryBean yamlMapFactoryBean = new YamlMapFactoryBean();
        yamlMapFactoryBean.setResources(new ClassPathResource("application.yaml"));
        System.out.println(yamlMapFactoryBean.getObject());
    }

    @Test
    public void testYamlToJson(){
        YamlJsonParser yamlJsonParser = new YamlJsonParser();
        Map<String,Object> map = yamlJsonParser.parseMap("{\n" +
                "  \"server\": {\n" +
                "    \"port\": 8080\n" +
                "  },\n" +
                "  \"my\": {\n" +
                "    \"def\": {\n" +
                "      \"addr\": \"localhost:9876\"\n" +
                "    }\n" +
                "  }\n" +
                "}");
        System.out.println(map);
    }

总结

yaml实际上格式很简洁易懂,而且配置容易,但是原生map使用是很痛苦的,是个缩进map,一般还是转为properties使用。yaml转properties实际上没必要自己写,Spring有现成的API,直接调用即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值