application.properties: k=v
application.yml: yaml aren’t markup language,不是一个标记文档
注意:1. k:空格v(或行内写法) 2.通过垂直对齐指定层次关系 3.默认可以不写引号,双引号中的转义字符会生效
application.properties:和application.yml两个配置文件可以相互代替,可以互补使用
例如:
(因为编辑器的原因,这里换为截图)
这里简单的写了一个测试小例子
student:
package com.example.demo.beans;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component // 将JavaBean放入spring容器
@ConfigurationProperties(prefix = "student") // 将该类和yml文件中的赋值绑定起来
public class Student {
private String name;
private int age;
private boolean sex;
private Date birthday;
private Map<String, Object> location;
private String[] hobbies;
private List<String> skills;
private Pet pet;
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;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Map<String, Object> getLocation() {
return location;
}
public void setLocation(Map<String, Object> location) {
this.location = location;
}
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
public List<String> getSkills() {
return skills;
}
public void setSkills(List<String> skills) {
this.skills = skills;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", location="
+ location + ", hobbies=" + Arrays.toString(hobbies) + ", skills=" + skills + ", pet=" + pet + "]";
}
}
pet:
package com.example.demo.beans;
public class Pet {
private String nickName;
private String strain;
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
@Override
public String toString() {
return "Pet [nickName=" + nickName + ", strain=" + strain + "]";
}
}
application.yml
student:
name: zs
age: 11
sex: true
birthday: 1990/01/01
location:
province: 陕西
city: 西安
zone: 莲湖区
# {province: 陕西,city: 西安,zone: 莲湖区}
hobbies: [足球,篮球]
# - 足球
# - 篮球
skills: [编程,金融]
# - 编程
# - 金融
pet: {nickName: 旺财,strain: 哈士奇}
# nickName: 旺财
# strain: 哈士奇
测试类:
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.demo.beans.Student;
@RunWith(SpringRunner.class)
@SpringBootTest
class DemoApplicationTests {
@Autowired
Student student;
@Test
void contextLoads() {
System.out.println(student);
}
}