SpringBoot这个配置文件中到底可以配置哪些东西
官方不推荐使用properties后缀文件,一般使用yaml文件(yaml文件和yml后缀两个是一样的,但是我的springboot2.5.4版本识别的是yml)
SpringBoot也支持yaml文件的导入
配置文件
SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的
application.properties
语法结构 :key=value
application.yml
语法结构 :key:空格 value
作用 :修改SpringBoot自动配置的默认值,因为SpringBoot在底层都给我们自动配置好了
修改端口为8081
yml(yaml)
YAML是 “YAML Ain’t a Markup Language” (YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)
这种语言以数据作为中心,而不是以标记语言为重点
传统xml配置:
<server>
<port>8081<port>
</server>
yaml配置:
server:
prot: 8080
yaml语法
语法要求严格!
1、空格不能省略
2、以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的。
3、属性和值的大小写都是十分敏感的。
字面量: 普通的值 [ 数字,布尔值,字符串 ]
字面量直接写在后面就可以 , 字符串默认不用加上双引号或者单引号;
k: v
注意:
“ ” 双引号,不会转义字符串里面的特殊字符 , 特殊字符会作为本身想表示的意思;
比如 :name: “k \n s” 输出 :k 换行 s
’ 单引号,会转义特殊字符 , 特殊字符最终会变成和普通字符一样输出
比如 :name: ‘k \n s’ 输出 :k \n s
对象、Map(键值对)
#对象、Map格式
k:
v1:
v2:
在下一行来写对象的属性和值得关系,注意缩进;比如:
student:
name: hhh
age: 3
行内写法
student: {name: hhh,age: 3}
数组( List、set )
用 - 值表示数组中的一个元素,比如:
pets:
- cat
- dog
- pig
行内写法
pets: [cat,dog,pig]
yaml注入配置文件
yaml文件更强大的地方在于,他可以给我们的实体类直接注入匹配值
先编写实体类
打印出来赋值成功
yaml注入
@ConfigurationProperties作用:
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定参数 prefix = “teacher” : 将配置文件中的teacher下面的所有属性一一对应
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Teacher{name='wp',
age=-764817502,
birth=Thu Dec 12 00:00:00 CST 1985,
online=true,
map={hh=123, k2=v2},
list=[code, music],
student=Student{name='sss_吕布', age=12}
}
Teacher{name='wp', age=23, birth=Thu Dec 12 00:00:00 CST 1985, online=true, map={hh=123, k2=v2}, list=[code, music], student=Student{name='hhh', age=12}}
package com.my.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
@Component//添加到Spring的组件中
public class Student {
private String name ;
private Integer age;
public Student() {
}
public Student(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 "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.my.pojo;
import com.sun.javafx.collections.MappingChange;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.*;
@Component
@ConfigurationProperties(prefix = "teacher")
public class Teacher {
private String name ;
private Integer age;
private Date birth;
private Boolean online;
private Map<String,Object> map;
private List<Object> list;
private Student student;
public Teacher() {
}
public Teacher(String name, Integer age, Date birth, Boolean online, Map<String, Object> map, List<Object> list, Student student) {
this.name = name;
this.age = age;
this.birth = birth;
this.online = online;
this.map = map;
this.list = list;
this.student = student;
}
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 Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Boolean getOnline() {
return online;
}
public void setOnline(Boolean online) {
this.online = online;
}
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 Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
", birth=" + birth +
", online=" + online +
", map=" + map +
", list=" + list +
", student=" + student +
'}';
}
}
加载指定的配置文件
@PropertySource :加载指定的配置文件;
@configurationProperties:默认从全局配置文件中获取值;
结论
1、@ConfigurationProperties只需要写一次即可 , @Value则需要每个字段都添加
2、松散绑定:这个什么意思呢?
比如我的yml中写的last-name,这个和lastName是一样的, - 后面跟着的字母默认是大写的。这就是松散绑定。
3、JSR303数据校验 , 可以在字段是增加一层过滤器验证 , 可以保证数据的合法性
4、复杂类型封装,yml中可以封装对象 , 使用value就不支持
配置yml和配置properties都可以获取到值 , 强烈推荐 yml;
如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下 @value;
如果说,我们专门编写了一个JavaBean来和配置文件进行一一映射,就直接@configurationProperties