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

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
  1. 配置对象数据
    语法:
    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:
    path: /demo1

注意:key1前面的空格个数不限定,在yml语法中,相同缩进代表同一个级别
3. 配置Map数据
语法:
map:
key: value1
key: value2

# map结构
map:
  key1: value1
  key2: value2
  1. 配置数组(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
常用的配置摘抄如下:

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

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

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

application.yml文件

server:
  port: 8888
  servlet:
    path: /demo

12.4 读取.yml配置文件

12.4.1 读取普通数据 key: value

  1. resources包下创建application.yml配置文件
    在这里插入图片描述
mail.host: acece.com2
  1. SpringBootReadThing.Test类中编写测试方法
    注入application.yml中的key, 使用@**${key}**注解注入
 @Value("${mail.host}")
    private String mailHost;

    @Test
    public void readYml(){
        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&"+mailHost);
    }

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

12.4.2 读取对象数据

  1. 配置文件信息如下
mail:
  host: acece.comNew
  port: port=80New
  username: username=啊策策New
  password: password=1234563New
  1. 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 {

    private String host;
    private String port;
    private String username;
    private String password;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "MailProperties{" +
                "host='" + host + '\'' +
                ", port='" + port + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
  1. SpringBootReadThing.Test类中编写测试方法
  @Autowired
    private MailProperties mailProperties;

    @Test
    public void readYml2(){
        System.out.println(mailProperties);;
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值