SpringBoot入门之SpringBoot读取yml配置文件, yml配置文件的语法 12

浅浅的花香味﹌ 2021-11-05 15:50 1281阅读 0赞

12. SpringBoot读取yml(yaml)文件

12.1 了解什么是yml(yaml)文件

YML文件格式是YAML (YAML Aint Markup Language)编写的文件格式,YAML是一种直观的能够被电脑识别的的数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持YAML库的不同的编程语言程序导入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。YML文件是以数据为核心的,比传统的xml方式更加简洁
YML文件的扩展名可以是.yml或.yaml

12.2 yml配置文件的语法

  1. 配置普通数据
    语法 key: value 注意,value之前一定要有个空格

    属性的配置

    name: acece

  2. 配置对象数据
    语法:
    key:
    ​ key1: value1
    ​ key2: value2
    或者
    key: {key1: value1,key2: value2}

    对象的配置

    person:
    name: acece
    age: 18
    addr: beijing

    行内对象配置

    person1: { name: acece,age: 18,addr: beijing}

    配置端口

    改变application.properties

    server:
    port: 8890
    servlet:

    1. path: /demo1

注意:key1前面的空格个数不限定,在yml语法中,相同缩进代表同一个级别

  1. 配置Map数据
    语法:
    map:
    key: value1
    key: value2

    map结构

    map:
    key1: value1
    key2: value2

  2. 配置数组(List, Set)数据
    语法:
    key:
    ​ - value1
    ​ - value2
    或者
    key: [value1,value2]

    配置数据集合

    city:

    • beijing
    • tianjin
    • shanghai
    • chongqing

    或者行内注入

    city: [beijing,tianjin,shanghai,chongqing]

    集合中的元素是对象形式

    student:

    • name: zhangsan
      age: 18
      score: 100
    • name: lisi
      age: 28
      score: 88
    • name: wangwu
      age: 38
      score: 90

    或者使用行内注入

    student: [{ name: zhangsan,age: 18,score: 100},{ name: lisi,age: 28,score: 88},{ name: wangwu,age: 38,score: 90}]

注意:value与之间的 - 之间存在一个空格

12.3 SpringBoot配置信息的查询

文档URL: https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#common-application-properties
常用的配置摘抄如下:

  1. # QUARTZ SCHEDULER (QuartzProperties)
  2. spring.quartz.jdbc.initialize-schema=embedded # Database schema initialization mode.
  3. spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
  4. spring.quartz.job-store-type=memory # Quartz job store type.
  5. spring.quartz.properties.*= # Additional Quartz Scheduler properties.
  6. # ----------------------------------------
  7. # WEB PROPERTIES
  8. # ----------------------------------------
  9. # EMBEDDED SERVER CONFIGURATION (ServerProperties)
  10. server.port=8080 # Server HTTP port.
  11. server.servlet.context-path= # Context path of the application.
  12. server.servlet.path=/ # Path of the main dispatcher servlet.
  13. # HTTP encoding (HttpEncodingProperties)
  14. spring.http.encoding.charset=UTF-8 # Charset of HTTP requests and responses. Added to the "Content-Type" header if not set explicitly.
  15. # JACKSON (JacksonProperties)
  16. spring.jackson.date-format= # Date format string or a fully-qualified date format class name. For instance, `yyyy-MM-dd HH:mm:ss`.
  17. # SPRING MVC (WebMvcProperties)
  18. spring.mvc.servlet.load-on-startup=-1 # Load on startup priority of the dispatcher servlet.
  19. spring.mvc.static-path-pattern=/** # Path pattern used for static resources.
  20. spring.mvc.view.prefix= # Spring MVC view prefix.
  21. spring.mvc.view.suffix= # Spring MVC view suffix.
  22. # DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
  23. spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
  24. spring.datasource.password= # Login password of the database.
  25. spring.datasource.url= # JDBC URL of the database.
  26. spring.datasource.username= # Login username of the database.
  27. # JEST (Elasticsearch HTTP client) (JestProperties)
  28. spring.elasticsearch.jest.password= # Login password.
  29. spring.elasticsearch.jest.proxy.host= # Proxy host the HTTP client should use.
  30. spring.elasticsearch.jest.proxy.port= # Proxy port the HTTP client should use.
  31. spring.elasticsearch.jest.read-timeout=3s # Read timeout.
  32. spring.elasticsearch.jest.username= # Login username.

我们可以通过配置application.poperties 或者 application.yml 来修改SpringBoot的默认配置
例如:
application.properties文件

  1. server.port=8888
  2. server.servlet.context-path=/demo

application.yml文件

  1. server:
  2. port: 8888
  3. servlet:
  4. path: /demo

12.4 读取.yml配置文件

12.4.1 读取普通数据 key: value

  1. resources包下创建application.yml配置文件
    在这里插入图片描述

    mail.host: acece.com2

  2. SpringBootReadThing.Test类中编写测试方法
    注入application.yml中的key, 使用@**${key}**注解注入

    @Value(“${mail.host}”)

    1. private String mailHost;
    2. @Test
    3. public void readYml(){
    4. System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&"+mailHost);
    5. }

在这里插入图片描述
控制台打印出来value值, 读取成功~

12.4.2 读取对象数据

  1. 配置文件信息如下

    mail:
    host: acece.comNew
    port: port=80New
    username: username=啊策策New
    password: password=1234563New

  2. MailProperties.java
    不需要指定application.yml的位置.

    package com.acece.domain;

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;

    /* @author 啊策策 @date 2019-08-06 19:09 /
    @ConfigurationProperties(prefix = “mail”) //配置文件的前缀
    //@PropertySource(value = “classpath:mail.properties”) //写明自定义的配置文件路径
    @Configuration //包扫描,springBoot启动自动创建该mailproperties对象
    public class MailProperties {

    1. private String host;
    2. private String port;
    3. private String username;
    4. private String password;
    5. public String getHost() {
    6. return host;
    7. }
    8. public void setHost(String host) {
    9. this.host = host;
    10. }
    11. public String getPort() {
    12. return port;
    13. }
    14. public void setPort(String port) {
    15. this.port = port;
    16. }
    17. public String getUsername() {
    18. return username;
    19. }
    20. public void setUsername(String username) {
    21. this.username = username;
    22. }
    23. public String getPassword() {
    24. return password;
    25. }
    26. public void setPassword(String password) {
    27. this.password = password;
    28. }
    29. @Override
    30. public String toString() {
    31. return "MailProperties{" +
    32. "host='" + host + '\'' +
    33. ", port='" + port + '\'' +
    34. ", username='" + username + '\'' +
    35. ", password='" + password + '\'' +
    36. '}';
    37. }

    }

  3. SpringBootReadThing.Test类中编写测试方法

    @Autowired

    1. private MailProperties mailProperties;
    2. @Test
    3. public void readYml2(){
    4. System.out.println(mailProperties);;
    5. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,1281人围观)

还没有评论,来说两句吧...

相关阅读