• SpringBoot——yml配置文件的书写和读取


    之前一直用的application.properties配置文件,只能是KV结构,后来的yml配置文件更像是树状结构,支持层级,比properties更灵活;

    yml配置规则

    属性跟属性值之间使用“:”和一个“空格”隔开,层级结构通过缩进对齐,缩进只能使用空格,不能用tab,并且大小写敏感,使用#注释文档;

    yml配置除了能像properties读取为kv结构,还能方便的读取成集合(List、Set等)、数组、对象、Map,还能进行嵌套结构读取;

    普通的kv读取:

    普通kv结构可以直接使用@Value标签读取,@Value除了可以读取普通kv,也能读取List或Map结构里的某一项;另外使用Environment.getProperty()方法也能像properties那样读取kv结构,但也只能以kv结构读取;

    yml格式:

    1. xxx: xxxx
    2. xxx:
    3. xxx: xxxx
    4. xxx:
    5. xxx: xxxx
    6. xxx: xxxx
    7. xxx: xxxx

     可以直接使用@Value("${xxx}")、@Value("${xxx.xxx.xxx}")读取;

    读取到集合和数组:

    使用@Value直接读取好像只能使用xxx: xxxx,xxxx,xxxx逗号隔开的格式,可以读取成List、Set或者数组,其他格式可以放到Bean里当作类的属性读取;

    yml格式:

    1. xxx:
    2. - xxxx
    3. - xxxx
    4. - xxxx
    5. xxx: [xxxx,xxxx,xxxx]
    6. xxx: xxxx,xxxx,xxxx

    读取为对象和Map:

    对象和Map的格式是一样的,只是读取的目标不同;读取为对象时需要提供setXXX方法,并且XXX要跟配置文件里命名一致;(另外测试时发现自动生成的set方法,对驼峰结构修改大小写会造成无法识别到配置)

    yml格式:

    1. xxx:
    2. x: xxxx
    3. x: xxxx
    4. x: xxxx
    5. xxx: {x: xxxx, x: xxxx, x: xxxx}

    另外各种格式可以组合嵌套使用,例如list里使用map或者map里包含list;

    Demo:

    yml配置文件application.yml:

    1. #yml文件
    2. a: hello
    3. val:
    4. b: 123
    5. c: false
    6. d: ENV
    7. mybeantest:
    8. name: Tom
    9. age: 14
    10. addr: 北京
    11. bestfriend: ${myArrayA[3]}
    12. mybeantest2: {name: Jerry, age: 13, id: 1001}
    13. myArrayA:
    14. - tom
    15. - jerry
    16. - jack
    17. - alice
    18. myArrayB: [a,b,c]
    19. myListC: 3,4,5,6
    20. myListD: [{name: Jerry, age: 11, addr: 上海},{name: Jack, age: 12, addr: 北京}]
    21. myListE:
    22. - {name: Bob, age: 22}
    23. - {name: Lily, age: 21}
    24. myListF:
    25. - name: AAA
    26. age: 11
    27. - name: BBB
    28. age: 22
    29. myListG:
    30. - - aaa
    31. - bbb
    32. - ccc
    33. - - dd
    34. - ee
    35. - ff
    36. maps:
    37. mapA:
    38. A: 1
    39. B: 2
    40. C: 3
    41. D: 4
    42. mapB:
    43. a: AA
    44. b: BB
    45. c: CC
    46. mapC: {X: x, Y: y, Z: z}
    47. mapD:
    48. L1:
    49. - x
    50. - y
    51. - z
    52. L2:
    53. - X
    54. - Y
    55. - Z
    56. mapE:
    57. M1:
    58. a: A
    59. b: B
    60. M2:
    61. C: c
    62. D: d
    63. mapF:
    64. m1: {name: TOM, age: 33}
    65. m2: {name: JERRY, age: 44}

    读取为bean的类MyBean.java和MyBean2.java:

    1. /**
    2. * 2022年10月25日下午5:57:43
    3. */
    4. package testspringboot.test3;
    5. import org.springframework.boot.context.properties.ConfigurationProperties;
    6. import org.springframework.stereotype.Component;
    7. /**
    8. * @author XWF
    9. *
    10. */
    11. @Component
    12. @ConfigurationProperties(prefix = "mybeantest")
    13. public class MyBean {
    14. public String name;
    15. public int age;
    16. public String address;
    17. public String bestFriend;
    18. public String getName() {
    19. return name;
    20. }
    21. public void setName(String name) {
    22. this.name = name;
    23. }
    24. public int getAge() {
    25. return age;
    26. }
    27. public void setAge(int age) {
    28. this.age = age;
    29. }
    30. public String getAddress() {
    31. return address;
    32. }
    33. public void setAddr(String address) {//setXXX方法,只需XXX跟配置文件里的名称对应即可(自动生成的set方法驼峰结构修改大小写会造成干扰)
    34. this.address = address;
    35. }
    36. public String getBestFriend() {
    37. return bestFriend;
    38. }
    39. public void setBestFriend(String bestFriend) {
    40. this.bestFriend = bestFriend;
    41. }
    42. @Override
    43. public String toString() {
    44. return "MyBean [name=" + name + ", age=" + age + ", address=" + address + ", bestFriend=" + bestFriend + "]";
    45. }
    46. }
    1. /**
    2. * 2022年10月27日上午9:45:35
    3. */
    4. package testspringboot.test3;
    5. import org.springframework.boot.context.properties.ConfigurationProperties;
    6. import org.springframework.stereotype.Component;
    7. /**
    8. * @author XWF
    9. *
    10. */
    11. @Component
    12. @ConfigurationProperties(prefix = "mybeantest2")
    13. public class MyBean2 {
    14. public String name;
    15. public int age;
    16. public int id;
    17. public String getName() {
    18. return name;
    19. }
    20. public void setName(String name) {
    21. this.name = name;
    22. }
    23. public int getAge() {
    24. return age;
    25. }
    26. public void setAge(int age) {
    27. this.age = age;
    28. }
    29. public int getId() {
    30. return id;
    31. }
    32. public void setId(int id) {
    33. this.id = id;
    34. }
    35. @Override
    36. public String toString() {
    37. return "MyBean2 [name=" + name + ", age=" + age + ", id=" + id + "]";
    38. }
    39. }

    读取为集合的类MyList.java:

    1. /**
    2. * 2022年10月26日上午10:06:10
    3. */
    4. package testspringboot.test3;
    5. import java.util.List;
    6. import java.util.Set;
    7. import org.springframework.boot.context.properties.ConfigurationProperties;
    8. import org.springframework.stereotype.Component;
    9. /**
    10. * @author XWF
    11. *
    12. */
    13. @Component
    14. @ConfigurationProperties(prefix = "")
    15. public class MyList {
    16. public List myArrayA;
    17. public Set myArrayB;
    18. public Integer[] myListC;
    19. public List myListD;
    20. public List myListE;
    21. public List myListF;
    22. public List> myListG;
    23. public List getMyArrayA() {
    24. return myArrayA;
    25. }
    26. public void setMyArrayA(List myArrayA) {
    27. this.myArrayA = myArrayA;
    28. }
    29. public Set getMyArrayB() {
    30. return myArrayB;
    31. }
    32. public void setMyArrayB(Set myArrayB) {
    33. this.myArrayB = myArrayB;
    34. }
    35. public Integer[] getMyListC() {
    36. return myListC;
    37. }
    38. public void setMyListC(Integer[] myListC) {
    39. this.myListC = myListC;
    40. }
    41. public List getMyListD() {
    42. return myListD;
    43. }
    44. public void setMyListD(List myListD) {
    45. this.myListD = myListD;
    46. }
    47. public List getMyListE() {
    48. return myListE;
    49. }
    50. public void setMyListE(List myListE) {
    51. this.myListE = myListE;
    52. }
    53. public List getMyListF() {
    54. return myListF;
    55. }
    56. public void setMyListF(List myListF) {
    57. this.myListF = myListF;
    58. }
    59. public List> getMyListG() {
    60. return myListG;
    61. }
    62. public void setMyListG(List> myListG) {
    63. this.myListG = myListG;
    64. }
    65. @Override
    66. public String toString() {
    67. return "MyList [myArrayA=" + myArrayA + ",\n myArrayB=" + myArrayB + ",\n myListC=" + myListC + ",\n myListD="
    68. + myListD + ",\n myListE=" + myListE + ",\n myListF=" + myListF + ",\n myListG=" + myListG + "]";
    69. }
    70. }

    读取为Map的类MyMap.java:

    1. /**
    2. * 2022年10月26日下午4:35:02
    3. */
    4. package testspringboot.test3;
    5. import java.util.List;
    6. import java.util.Map;
    7. import org.springframework.boot.context.properties.ConfigurationProperties;
    8. import org.springframework.stereotype.Component;
    9. /**
    10. * @author XWF
    11. *
    12. */
    13. @Component
    14. @ConfigurationProperties(prefix = "maps")
    15. public class MyMap {
    16. public Map mapA;
    17. public Map mapB;
    18. public Map mapC;
    19. public Map> mapD;
    20. public Map> mapE;
    21. public Map mapF;
    22. public Map getMapA() {
    23. return mapA;
    24. }
    25. public void setMapA(Map mapA) {
    26. this.mapA = mapA;
    27. }
    28. public Map getMapB() {
    29. return mapB;
    30. }
    31. public void setMapB(Map mapB) {
    32. this.mapB = mapB;
    33. }
    34. public Map getMapC() {
    35. return mapC;
    36. }
    37. public void setMapC(Map mapC) {
    38. this.mapC = mapC;
    39. }
    40. public Map> getMapD() {
    41. return mapD;
    42. }
    43. public void setMapD(Map> mapD) {
    44. this.mapD = mapD;
    45. }
    46. public Map> getMapE() {
    47. return mapE;
    48. }
    49. public void setMapE(Map> mapE) {
    50. this.mapE = mapE;
    51. }
    52. public Map getMapF() {
    53. return mapF;
    54. }
    55. public void setMapF(Map mapF) {
    56. this.mapF = mapF;
    57. }
    58. @Override
    59. public String toString() {
    60. return String.format("MyMap [mapA=%s,\n mapB=%s,\n mapC=%s,\n mapD=%s,\n mapE=%s,\n mapF=%s]", mapA, mapB, mapC, mapD,
    61. mapE, mapF);
    62. }
    63. }

    进行测试用的类Test3Class.java:

    1. /**
    2. * 2022年10月25日下午4:02:58
    3. */
    4. package testspringboot.test3;
    5. import java.util.List;
    6. import java.util.Set;
    7. import javax.annotation.PostConstruct;
    8. import javax.annotation.Resource;
    9. import org.springframework.beans.factory.annotation.Autowired;
    10. import org.springframework.beans.factory.annotation.Value;
    11. import org.springframework.core.env.Environment;
    12. import org.springframework.stereotype.Component;
    13. /**
    14. * @author XWF
    15. *
    16. */
    17. @Component
    18. public class Test3Class {
    19. @Value("${a}")
    20. public String a;
    21. @Value("${val.b}")
    22. public int b;
    23. @Value("${val.c}")
    24. public boolean c;
    25. @Autowired
    26. public Environment env;
    27. @Resource
    28. public MyBean bean;
    29. @Resource
    30. public MyBean2 bean2;
    31. @Value("${myArrayA[3]}")
    32. public String arrayA_3;
    33. @Value("${myArrayB[1]}")
    34. public String arrayB_1;
    35. @Value("${myListC}")
    36. public List list;
    37. @Value("${myListC}")
    38. public Set set;
    39. @Value("${myListC}")
    40. public String[] array;
    41. @Resource
    42. public MyList mylist;
    43. @Resource
    44. public MyMap mymap;
    45. @PostConstruct
    46. public void fun() {
    47. System.out.println(a);
    48. System.out.println(b);
    49. System.out.println(c);
    50. System.out.println(env.getProperty("val.d"));
    51. System.out.println(bean);
    52. System.out.println(bean2);
    53. System.out.println(arrayA_3);
    54. System.out.println(arrayB_1);
    55. System.out.println(list);
    56. System.out.println(set);
    57. System.out.println(array);
    58. System.out.println(mylist);
    59. System.out.println(mymap);
    60. }
    61. }

    启动类(只测配置文件,所以设置WebApplicationType.NONE关闭web服务):

    1. /**
    2. * 2022年10月25日下午2:53:50
    3. */
    4. package testspringboot.test3;
    5. import org.springframework.boot.SpringApplication;
    6. import org.springframework.boot.WebApplicationType;
    7. import org.springframework.boot.autoconfigure.SpringBootApplication;
    8. /**
    9. * @author XWF
    10. *
    11. */
    12. @SpringBootApplication
    13. public class Test3Main {
    14. /**
    15. * @param args
    16. */
    17. public static void main(String[] args) {
    18. SpringApplication sapp = new SpringApplication(Test3Main.class);
    19. sapp.setWebApplicationType(WebApplicationType.NONE);
    20. sapp.run(args);
    21. }
    22. }

    执行结果:

  • 相关阅读:
    web网页设计期末课程大作业:家乡旅游主题网站设计——河北8页HTML+CSS+JavaScript
    SpringBoot基础(六)-- 辅助功能之一 -- 内嵌tomcat
    伦敦金区间操作容易碰到的问题
    运放噪声频率曲线-运放噪声计算-运算放大器
    精准测试的相关概念与实践
    【图神经网络论文整理】(二)—— HOW ATTENTIVE ARE GRAPH ATTENTION NETWORKS?:GATv2
    Replication(上):常见复制模型&分布式系统挑战
    mac 清除 iTerm2 终端屏幕内容
    Tomcat生产部署多个应用
    Kfka监控工具--Kafka-eagle安装
  • 原文地址:https://blog.csdn.net/FlyLikeButterfly/article/details/127556553