YAML的设计者认为在配置文件中所要表达的数据内容有三种类型:标量(Scalar,如字符串和整数等)、序列(Sequence,如数组)和Mapping(类似hash的key/value pair)。

一、读取键值对

  1. \\customer.yaml
  2. firstName: "John"
  3. lastName: "Doe"
  4. age: 20
  1. Yaml yaml = new Yaml();
  2. Object result= yaml.load(new FileReader("D:\\config\\customer.yaml"));

result加载为LinkedHashMap类。

二、写入键值对

  1. Map<String,Object> object = new LinkedHashMap<>();
  2. object.put("firstName","John");
  3. object.put("lastName","Doe");
  4. object.put("age",20);
  5. Yaml yaml = new Yaml();
  6. yaml.dump(object,new FileWriter("D:\\config\\object.yaml"));
  7. return object.toString();
  1. \\object.yaml
  2. {firstName: John, lastName: Doe, age: 20}

三、写入java对象

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. public class Pojo {
  5. public int id;
  6. public String name;
  7. }
  1. Yaml yaml = new Yaml();
  2. List<Pojo> arr = new ArrayList<>();
  3. Pojo p1 = new Pojo(1,"hello");
  4. Pojo p2 = new Pojo(2,"world");
  5. arr.add(p1);
  6. arr.add(p2);
  7. yaml.dump(arr,new FileWriter("D:\\config\\pojo.yaml"));
  1. \\pojo.yaml
  2. - !!com.example.yamldemo.domain.Pojo {id: 1, name: hello}
  3. - !!com.example.yamldemo.domain.Pojo {id: 2, name: world}

四、读取复杂键值对

  1. \\application
  2. spring:
  3. profiles:
  4. active: instance1
  5. application:
  6. name: eureka-service
  7. eureka:
  8. client:
  9. #由于该应用为注册中心,设置为false,表明不向注册中心注册自己
  10. register-with-eureka: false
  11. server:
  12. enable-self-preservation: false
  13. #是否从eureka服务器获取注册信息,这里不需要
  14. fetch-registry: false
  15. logging:
  16. level:
  17. com:
  18. netflix:
  19. eureka: OFF
  20. discovery: OFF
  21. management:
  22. endpoints:
  23. web:
  24. exposure:
  25. include: '*'
  26. endpoint:
  27. health:
  28. show-details: ALWAYS
  29. ---
  30. spring:
  31. profiles: instance1
  32. server:
  33. port: 8761
  34. eureka:
  35. client:
  36. service-url:
  37. defaultZone: http://localhost:8762/eureka/
  38. ---
  39. spring:
  40. profiles: instance2
  41. server:
  42. port: 8762
  43. eureka:
  44. client:
  45. service-url:
  46. defaultZone: http://localhost:8761/eureka/
  1. Yaml yaml = new Yaml();
  2. Iterable<Object> result = yaml.loadAll(new FileReader("D:\\config\\application.yaml"));
  3. log.info(result.getClass());
  4. for(Object object:result){
  5. log.info(object.getClass());
  6. for(Map.Entry entry:((LinkedHashMap<Object,Object>)object).entrySet()){
  7. log.info(entry.getKey());
  8. log.info(entry.getKey().getClass());
  9. log.info(entry.getValue());
  10. log.info(entry.getValue().getClass());
  11. }
  12. }

由输出可得它们都是>类型

五、读取java对象

  1. \\address.yaml
  2. lines: |
  3. 458 Walkman Dr.
  4. Suite #292
  5. city: Royal Oak
  6. state: MI
  7. postal: 48046
  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. public class Address {
  5. private String lines;
  6. private String city;
  7. private String state;
  8. private Integer postal;
  9. }
  1. Yaml yaml = new Yaml();
  2. Address ret = yaml.loadAs(new FileReader("D:\\config\\address.yaml"), Address.class);
  3. log.info(ret.getCity());
  4. return ret.toString();

注意

把yaml文件放入项目的时候犯了大错,文件夹定义成了config,里面又有application.yaml,所以配置文件加载错了

因此我去了解下yaml、properties文件加载的优先级,最高优先级是项目目录下的config文件夹里面的application,第二就是项目目录下的application,第三就是resource下的config文件夹里的application,第四就是resource的application,还有一个规则就是application.properties加载优先级比application.yaml优先级高。