目录
五、yml(yet another markup language)配置文件
一、配置文件的作用
整个项目中所有重要的数据都是存储在配置文件中。比如:会存储数据库的连接信息(用户名和密码)、项目的启动端口、第三方系统的调用密钥等信息、用于发现和定位问题的普通日志和异常日志等。总而言之,配置文件非常重要。
二、安装插件Spring Tools
IDEA 社区版安装 Spring Assistant 插件之后,就可以正常创建 Spring Boot 项⽬了,并且 yml 的配置文件就有提示了。但默认情况下是不⽀持 properties 格式的⽇志提示的,这个时候需要安装了 Spring Tools 插件才会有相应的提示。
三、配置文件的分类
主要分为两种:.properties 和 .yml。
理论上来讲,properties和yml可以存在于一个项目中。当在同一个项目中时,如果配置文件中出现了同样的配置,以properties为主。总结为:.properties配置文件的优先级最高,当加载完.properties文件之后,也会加载.yml文件的配置信息。
假设,application.properties中设置端口号为8089,application.yml中设计端口号为8088,程序在启动时占用的端口号时8089.
虽然理论上可以以上两种配置文件可以共存,但是在实际的业务中,会采用的一种统一的配置文件格式,这样可以更好的维护。
四、properties配置文件
4.1 properties基本语法
properties配置文件是最早期的配置文件格式,也是创建Spring Boot项目默认的配置文件。
以键值对的行驶配置,键和值之间以=连接。不能加空格。
#端口号
server.port=8089
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db
spring.datasource.username=root
spring.datasource.password=1111
4.2 读取配置文件(@Value)
使用@Value注解来读取配置里面的内容
@Value("${键}")
package com.example.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@ResponseBody // 设置返回的不是页面,而是内容
@Controller // 将对象存入Spring
public class dataBaseController {
// 读取url
@Value("${spring.datasource.url}")
private String url;
@RequestMapping("/url")
public String getUrl(){
return url;
}
// 读取username
@Value("${spring.datasource.username}")
private String userName;
@RequestMapping("/username")
public String getUserName(){
return userName;
}
}
4.3 properties缺点分析
properties中有很多冗余的信息。
要想解决这个问题,就需要使用yml配置文件的格式化了。
五、yml(yet another markup language)配置文件
5.1 yml基本语法
yml是树形结构的配置文件,他的基础语法是key: value,注意,冒号后面有个空格,这个空格不能省略。
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/testdb
username: root
password: 1111
与properties相比,yml的写法更简单明了。
注意事项:①
②
字符串默认不⽤加上单引号或者双引号。 单引号会转义特殊字符,特殊字符最终只是⼀个普通的字符串数据。 双引号不会转义字符串⾥⾯的特殊字符;特殊字符会作为本身想表示的意思。
package com.example.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.PostConstruct;
@ResponseBody // 设置返回的不是页面,而是内容
@Controller // 将对象存入Spring
public class dataBaseController {
@Value("${string.value1}")
private String value1;
@Value("${string.value2}")
private String value2;
@Value("${string.value3}")
private String value3;
@PostConstruct
public void postConstruct(){
System.out.println(value1);
System.out.println(value2);
System.out.println(value3);
}
}
5.2 yml读取数据(@Value)
和properties一样,使用@Value来读取。
package com.example.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@ResponseBody // 设置返回的不是页面,而是内容
@Controller // 将对象存入Spring
public class dataBaseController {
// 读取url
@Value("${spring.datasource.url}")
private String url;
@RequestMapping("/url")
public String getUrl(){
return url;
}
// 读取username
@Value("${spring.datasource.username}")
private String userName;
@RequestMapping("/username")
public String getUserName(){
return userName;
}
}
5.3 yml特点
可读性更高
写法更简单明了
可以表示更复杂的数据结构(对象、数组)
通用性更好可以跨语言(Java、python、Golang)
5.4 yml进阶
5.4.1 yml配置不同的数据类型及null
#字符串
string:
value1: 你好 \n hello
value2: '你好 \n hello'
value3: "你好 \n hello"
# 布尔值
boolean.value1: true
boolean.value2: false
#整数
int.value1: 10
inta.value2: 1010_1111
#浮点数
float.value1: 3.12345
#Null,~表示null
null.value: ~
package com.example.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.PostConstruct;
@ResponseBody // 设置返回的不是页面,而是内容
@Controller // 将对象存入Spring
public class dataBaseController {
@Value("${string.value1}")
private String value1;
@Value("${string.value2}")
private String value2;
@Value("${string.value3}")
private String value3;
@Value("${int.value1}")
private int value11;
@Value("${float.value1}")
private float value33;
@Value("${boolean.value1}")
private boolean bvalue1l;
@PostConstruct
public void postConstruct(){
System.out.println(value1);
System.out.println(value2);
System.out.println(value3);
System.out.println(value11);
System.out.println(value33);
}
}
5.4.2 配置对象
配置对象的代码:
student:
id: 1001
name: zhangsan
age: 22
student2: {id: 1002,name: lisi,age: 23}
读取时不能使用@Value来读取配置中的对象,需要使用另外一个注解@ConfigurationProperties来读取。注意,getter和setter不能省略。
package com.example.component;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "student")
public class StudentComponent {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 "StudentComponent{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
调用类的代码:
package com.example.controller;
import com.example.component.StudentComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.annotation.PostConstruct;
@Controller
public class ReadStudent {
@Autowired
private StudentComponent studentComponent;
@PostConstruct
private void showStudent(){
System.out.println(studentComponent);
}
}
执行结果:
5.4.3 配置数组
studenttypes:
name:
- lisi
- zhangsan
- wangwu
studenttype2: {name:[lisi,zhangsan,wangwu]}
package com.example.component;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties("studenttypes")
public class StudentTypeComponet {
List<String> name;
public List<String> getName(){return name;}
public void setName(List<String> name){this.name = name;}
}
package com.example.controller;
import com.example.component.StudentTypeComponet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.annotation.PostConstruct;
@Controller
public class ReadStudentType {
@Autowired
private StudentTypeComponet studentTypeComponet;
@PostConstruct
public void show(){
System.out.println(studentTypeComponet.getName());
}
}
运行结果:
5.4.4 查看更多系统配置项
Common Application Properties (spring.io)
六、总结
properties和yml是常用的两种配置文件。对这两种配置文件对比总结如下:
properties的优先级高于yml。
properties键和值用=隔开,yml键和值用: 隔开。
properties和yml都使用@Value读取配置文件中设置的属性。
properties冗余较高,yml简介明了。
yml可以表示更复杂的数据结构,在读取配置的对象和数组时使用@ConfigurationProperties来读取,读取对象getter和setter不能省略,读取数组set不能省略。