官方提供两种配置文件分别是application.properties与application.yaml文件
application.properties文件语法结构为key=value
例 : server.port=8080
存对象
student.name=zhangsan
student.age=3
application.yaml文件语法结构为key:空格value
例 :
server: port: 8080
存对象
student: name: zhangsan age: 3
行内写法
对象:
student: {name: zhangsan,age: 3}
数组
arrar: -a -b -c
或者 array: [a,b,c] 而properties只能存键值对但是yaml对空格的要求非常高!
但是yaml可以给实体类赋值
实例:Cat实体类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Cat {
@Value("招财")
private String name;
@Value("1")
private Integer age;
public Cat() {
}
public Cat(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
porple实体类
@CoanficqurationPrepertiea作用:
将配置文件中配置的每一个屈性的值,映射到这个组件中;
告诉springBoot将本类中的所有属性和配置文件中相关的配管进行绑定
参数 prcfix - "people”:将配置文件中的people下面的所有属性一 一对应
只有这个组件是容器中的组件,才能使用容器提供的@ConfiqurationPrcpertiea功能
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "people")
public class People {
private String name;
private Integer age;
private boolean happy;
private Date birth;
private Map<String,Object> map;
private List<Object> list;
private Cat cat;
public People() {
}
public People(String name, Integer age, boolean happy, Date birth, Map<String, Object> map, List<Object> list, Cat cat) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.map = map;
this.list = list;
this.cat = cat;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isHappy() {
return happy;
}
public void setHappy(boolean happy) {
this.happy = happy;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birth=" + birth +
", map=" + map +
", list=" + list +
", cat=" + cat +
'}';
}
}
application.yaml配置文件注意 application.yaml中的字段名对应实体类的列名
people:
name: zhangsan
age: 13
happy: true
birth: 2022/3/25
map: {aa: a,bb: b,cc: c}
list: [a,b,c,d,e,f]
cat:
name: zhaocai
age: 3
使用xx.properties文件可以通过类名上使用 @PropertySource("classpath:xx.properties")
通过列名上使用@Value("${xxx}")来进行赋值也就是SPEL表达式