yaml文件的使用
配置文件分类
SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用application.properties或者application.yml(application.yaml)进行配置。
-
properties:
server.port=8080
键值对的方式
-
yml:
server: port: 8080
树形结构,冒号 : 和 ‘值’ 之前要有一个空格
-
默认配置文件名称:application
-
在同级目录下优先级:properties > yml >yaml
YAML
YAML全称是YAML Ain’t Markup Language,它以数据为核心,比传统的xml方式更加简洁。
YAML的文件扩展名可以使用.yml
或者.yaml
对比properties和xml
- properties:
没有层级的层次感server.port=8080 server.address=127.0.0.1
- xml:
直观,但是标签写起来比较繁琐<server> <port>8080</port> <address>127.0.01</address> </server>
- yml:
简洁,有缩进server: port: 8080 address: 127.0.0.1
基本语法
- 大小写敏感
- 数据值前边必须有空格,作为分隔符
- 使用缩进表示层级关系
- 缩进时不允许视同tab键,只允许使用空格(因为各个系统的tab对应的空格数目可能不同,导致层次混乱)
- 缩进的空格数目不重要,都会被解析器忽略
#
表示注释
server:
port: 8080
address: 127.0.0.1
数据格式
-
对象(map) :键值对的集合
person: name: zhangsan # 行内写法 person: {name: zhangsan}
-
数组:一组按次序排列的值,使用"-"表示每个元素
address: - hainan - shanghai # 行内写法 address: [hainan, shanghai]
-
纯量:单个的、不可再分的值
msg1: 'hello \n world' # 单引忽略转义字符 msg2: "hello \n world" # 双引识别转义字符
-
参数引用 ${key}
name: zhangsan person: name: ${name} # 引用上面定义的值
SpringBoot读取配置内容
- 配置文件
server: port: 8082 name: abc # 对象 person: name: zhangsan # ${name} age: 20 person2: {name: zhangsan, age: 20} # 数组 address: - hainan - beijing address2: [hainan, beijing] # 纯量 msg1: 'hello \n world' msg2: "hello \n world"
@Value注解获取
- 测试文件
package com.cmy.springboothello2; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author 陈明勇 */ @RestController public class HelloController { @Value("${name}") private String name; @Value("${person.name}") private String name2; @Value("${person.age}") private int age; @Value("${address[0]}") private String address; @Value("${msg1}") private String msg1; @Value("${msg2}") private String msg2; @RequestMapping("/getAppContent") public String hello() { System.out.println(name); System.out.println(name2); System.out.println(age); System.out.println(address); System.out.println(msg1); System.out.println(msg2); return "hello SpringBoot222!"; } }
- 结果
abc zhangsan 20 hainan hello \n world hello world
Environment对象获取
注入Environment 对象,使用getProperty("变量名")
获取对应的值
- 测试文件
package com.cmy.springboothello2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author 陈明勇 */ @RestController public class HelloController { @Autowired private Environment env; @RequestMapping("/getAppContent") public String hello() { System.out.println(env.getProperty("name")); System.out.println(env.getProperty("person.name")); System.out.println(env.getProperty("person.age")); System.out.println(env.getProperty("address[1]")); System.out.println(env.getProperty("msg1")); System.out.println(env.getProperty("msg2")); return "hello SpringBoot222!"; } }
- 结果
abc zhangsan 20 beijing hello \n world hello world
@ConfigurationProperties获取
-
配置文件
server: port: 8082 # 对象 person: name: zhangsan # ${name} age: 20 address: - hainan - beijing
-
创建一个Person类,添加
@Component
和@ConfigurationProperties
注解,并指定前缀package com.cmy.springboothello2; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author 陈明勇 */ @Component @ConfigurationProperties(prefix = "person") public class Person { private String name; private int age; private String[] address; public String[] getAddress() { return address; } public void setAddress(String[] address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
-
测试文件
package com.cmy.springboothello2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author 陈明勇 */ @RestController public class HelloController { @Autowired private Person person; @RequestMapping("/getAppContent") public String hello() { System.out.println(person); String[] address = person.getAddress(); for (String s : address) { System.out.println(s); } return "hello SpringBoot222!"; } }
-
结果
Person{name='zhangsan', age=20} hainan beijing
-
修复
@ConfigurationProperties
注解出现的红条问题
添加以下pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
此依赖可以在编写配置文件时拥有提示作用