SpringBoot读取yaml配置

2018-11-06 16:23:32

SpringBoot读取yaml配置的值

application.yml中添加配置

  1. user:
  2. name: 张三
  3. age: 19
  4. sex:
  5. maps: {weight: 70,height: 170}
  6. address:
  7. addr: 重庆市渝中区
  8. lists:
  9. - 京东
  10. - 淘宝
  11. - 坑多多

新建Address类

  1. package com.atgenee.demo.config;
  2. public class Address {
  3. private String addr;
  4. public String getAddr() {
  5. return addr;
  6. }
  7. public void setAddr(String addr) {
  8. this.addr = addr;
  9. }
  10. @Override
  11. public String toString() {
  12. return "Address{" +
  13. "addr='" + addr + '\'' +
  14. '}';
  15. }
  16. }

新建User

  1. package com.atgenee.demo.config;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. import java.util.List;
  5. import java.util.Map;
  6. @Component
  7. @ConfigurationProperties(prefix = "user")
  8. public class User {
  9. private String name;
  10. private Integer age;
  11. private char sex;
  12. private Map<String,Integer> maps;
  13. private Address address;
  14. private List<String> lists;
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public Integer getAge() {
  22. return age;
  23. }
  24. public void setAge(Integer age) {
  25. this.age = age;
  26. }
  27. public char getSex() {
  28. return sex;
  29. }
  30. public void setSex(char sex) {
  31. this.sex = sex;
  32. }
  33. public Map<String, Integer> getMaps() {
  34. return maps;
  35. }
  36. public void setMaps(Map<String, Integer> maps) {
  37. this.maps = maps;
  38. }
  39. public Address getAddress() {
  40. return address;
  41. }
  42. public void setAddress(Address address) {
  43. this.address = address;
  44. }
  45. public List<String> getLists() {
  46. return lists;
  47. }
  48. public void setLists(List<String> lists) {
  49. this.lists = lists;
  50. }
  51. @Override
  52. public String toString() {
  53. return "User{" +
  54. "name='" + name + '\'' +
  55. ", age=" + age +
  56. ", sex=" + sex +
  57. ", maps=" + maps +
  58. ", address=" + address +
  59. ", lists=" + lists +
  60. '}';
  61. }
  62. }

新建测试类

  1. package com.atgenee.demo;
  2. import com.atgenee.demo.config.User;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. public class UserApplicationTests {
  11. //注入配置类
  12. @Autowired
  13. User user;
  14. @Test
  15. public void contextLoads() {
  16. System.out.println(user);
  17. }
  18. }

测试结果

  1. User{name='Administrator', age=19, sex=男, maps={weight=70, height=170}, address=Address{addr='重庆市渝中区'}, lists=[京东, 淘宝, 坑多多]}

注意

  • yaml格式要求严格,以键值形式配置,键后冒号后面的空格一定不要丢(如: username: admin

  • @ConfigurationProperties注解依赖

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-configuration-processor</artifactId>
    4. <optional>true</optional>
    5. </dependency>

0
1
0

添加评论

正在回复:
取消
0
0
1
0