springboot中對yaml文件的解析

  1、YAML是「YAML不是一種標記語言」的外語縮寫 (見前方參考資料原文內容);但爲了強調這種語言以數據作爲中心,而不是以置標語言爲重點,而用返璞詞從新命名。它是一種直觀的可以被電腦識別的數據序列化格式,是一個可讀性高而且容易被人類閱讀,容易和腳本語言交互,用來表達資料序列的編程語言。java

  2、在springboot中的基礎配置是經過yaml的方式來解析實現,固然也支持xml的文件解析。這裏主要是學習源碼中瞭解到yaml的解析過程。幫助本身在學習源碼的時候,斷定spring作了哪些操做。mysql

  3、yaml的配置文件spring

server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/model?useUnicode=true&characterEncoding=utf-8
    username: root
    password: root
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update

  4、yaml的解析過程sql

import org.springframework.core.io.Resource;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.reader.UnicodeReader;

import java.io.IOException;
import java.util.*;

public class YamlUtils {

    /**
     * 單個yaml文件處理
     * @param resource
     * @return
     * @throws IOException
     */
    public static Map<String, Object> yamlHandler(@NonNull Resource resource) throws IOException {
        //返回的結果
        Map<String, Object> result = new LinkedHashMap<>();
        //讀取方式
        UnicodeReader reader = new UnicodeReader(resource.getInputStream());
        //單文件處理
        Yaml yaml = new Yaml();
        Object object = yaml.load(reader);
        //這裏只是簡單處理,須要多個方式能夠本身添加
        if (object instanceof Map) {
            Map map = (Map) object;
            buildFlattenedMap(result, map, null);
        }
        reader.close();
        return result;
    }

    /**
     * 單個yaml文件處理
     * @param resources
     * @return
     * @throws IOException
     */
    public static Map<String, Object> yamlHandler(@NonNull Resource[] resources) throws IOException {

        //返回的結果
        Map<String, Object> result = new LinkedHashMap<>();
        Yaml yaml = new Yaml();
        //多個文件處理
        Iterator<Resource> iterator = Arrays.stream(resources).iterator();
        while (iterator.hasNext()) {
            Resource resource = iterator.next();
            UnicodeReader reader = new UnicodeReader(resource.getInputStream());
            Object object = yaml.load(reader);
            //這裏只是簡單處理,須要多個方式能夠本身添加
            if (object instanceof Map) {
                Map map = (Map) object;
                buildFlattenedMap(result, map, null);
            }
            reader.close();
        }
        return result;
    }

    /**
     * 這部分代碼來至springboot源碼部分對yaml的解析
     * YamlProcessor.java buildFlattenedMap方法
     * @param result
     * @param source
     * @param path
     */
    private static void buildFlattenedMap(Map<String, Object> result, Map<String, Object> source, @Nullable String path) {
        //循環讀取原數據
        source.forEach((key, value) -> {
            //若是存在路徑進行拼接
            if (StringUtils.hasText(path)) {
                if (key.startsWith("[")) {
                    key = path + key;
                } else {
                    key = path + '.' + key;
                }
            }
            //數據類型匹配
            if (value instanceof String) {
                result.put(key, value);
            } else if (value instanceof Map) {
                //若是是map,就繼續讀取
                Map<String, Object> map = (Map)value;
                buildFlattenedMap(result, map, key);
            } else if (value instanceof Collection) {
                Collection<Object> collection = (Collection)value;
                if (collection.isEmpty()) {
                    result.put(key, "");
                } else {
                    int count = 0;
                    Iterator var7 = collection.iterator();

                    while(var7.hasNext()) {
                        Object object = var7.next();
                        buildFlattenedMap(result, Collections.singletonMap("[" + count++ + "]", object), key);
                    }
                }
            } else {
                result.put(key, value != null ? value : "");
            }
        });
    }

}

   5、測試編程

   public static void main(String[] args) throws IOException {
        Map<String, Object> map = YamlUtils.yamlHandler(new ClassPathResource("config/application.yaml"));
        System.out.println(map);
    }

  

相關文章
相關標籤/搜索