springboot的核心配置文件
properties文件
application.properties springboot的核心配置文件
yaml文件
简介
YAML 是 “YAML Ain’t Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。
非常适合用来做以数据为中心的配置文件
基础语法(k: v)
-
key: value;kv之间有空格
-
大小写敏感
-
使用缩进表示层级关系
-
缩进不允许使用tab,只允许空格
-
缩进的空格数不重要,只要相同层级的元素左对齐即可
-
'#'表示注释
-
字符串无需加引号,如果要加,’'与""表示字符串内容 会被 转义/不转义
数据类型
- 面量:单个的、不可再分的值。date、boolean、string、number、null
k: v
- 对象:键值对的集合。map、hash、set、object
行内写法: k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3
- 数组:一组按次序排列的值。array、list、queue
行内写法: k: [v1,v2,v3]
#或者
k:
- v1
- v2
- v3
配置实例
绑定配置文件
@ConfigurationProperties(prefix = "person")//通过注解完成与配置文件的绑定操作
配置类
编写要配置的pojo类
package com.dzu.boot.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.*;
@ConfigurationProperties(prefix = "person")//通过注解完成与配置文件的绑定操作
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> salarys;
private Map<String, List<Pet>> allPets;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
public String[] getInterests() {
return interests;
}
public void setInterests(String[] interests) {
this.interests = interests;
}
public List<String> getAnimal() {
return animal;
}
public void setAnimal(List<String> animal) {
this.animal = animal;
}
public Map<String, Object> getScore() {
return score;
}
public void setScore(Map<String, Object> score) {
this.score = score;
}
public Set<Double> getSalarys() {
return salarys;
}
public void setSalarys(Set<Double> salarys) {
this.salarys = salarys;
}
public Map<String, List<Pet>> getAllPets() {
return allPets;
}
public void setAllPets(Map<String, List<Pet>> allPets) {
this.allPets = allPets;
}
public Person(String userName, Boolean boss, Date birth, Integer age, Pet pet, String[] interests, List<String> animal, Map<String, Object> score, Set<Double> salarys, Map<String, List<Pet>> allPets) {
this.userName = userName;
this.boss = boss;
this.birth = birth;
this.age = age;
this.pet = pet;
this.interests = interests;
this.animal = animal;
this.score = score;
this.salarys = salarys;
this.allPets = allPets;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"userName='" + userName + '\'' +
", boss=" + boss +
", birth=" + birth +
", age=" + age +
", pet=" + pet +
", interests=" + Arrays.toString(interests) +
", animal=" + animal +
", score=" + score +
", salarys=" + salarys +
", allPets=" + allPets +
'}';
}
}
依赖的类
package com.dzu.boot.pojo;
public class Pet {
private String name;
private Double weight;
public Pet(String name, Double weight) {
this.name = name;
this.weight = weight;
}
public Pet() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Pet{" +
"name='" + name + '\'' +
", weight=" + weight +
'}';
}
}
applicationn.yaml配置文件
#对象的配置方式
person:
userName: zhangsan
boss: false
birth: 2019/12/12 20:12:33
age: 18
#内置对象
pet:
name: tomcat
weight: 23.4
interests: [篮球,游泳] #数组的配置方式
animal: #list集合的配置方式
- jerry
- mario
score: #map双列集合的配置方式
english:
first: 30
second: 40
third: 50
salarys: [3999,4999.98,5999.99] #集合的行内配置方式
allPets:
sick:
- {name: tom}
- {name: jerry,weight: 47}
health: [{name: mario,weight: 47}]
实例测试(未完成)
配置提示
自定义的类和配置文件绑定一般没有提示。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>