SpringBoot 使用一个全局的配置文件,名字固定
application.properties 语法结构:key=value
application.yml 语法结构:key:(空格)value
配置文件的作用是可以修改 SpringBoot 自动配置的默认值
在 resources 目录下创建 application.yml
server:
port: 8080
properties 只能保存键值对
yaml 对空格的要求非常严苛,并且 yaml 可以注入到配置类
举个例子
Dog.java:
package com.demo.helloworld.pojo;
public class Dog {
private String name;
private Integer age;
public Dog() {
}
public Dog(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 "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Person.java:
@ConfigurationProperties 作用:
将配置文件中配置的每一个属性的值,映射到这个组件中
告诉 SpringBoot 将本类中的所有属性和配置文件中相关的配置进行绑定
参数 prefix = "person" 将配置文件中的 person 类下的所有属性一一对应
只有这个组件是容器中的组件,才能使用容器提供的 @ConfigurationProperties 功能
package com.demo.helloworld.pojo;
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 //注册bean
@ConfigurationProperties(prefix = "person") //配置类的值与配置文件绑定
public class Person {
private String name;
private Integer age;
private Boolean happy;
private Date birthday;
private Map<String, Object> map;
private List<Object> list;
private Dog dog;
public Person() {
}
public Person(String name, Integer age, Boolean happy, Date birthday, Map<String, Object> map, List<Object> list, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birthday = birthday;
this.map = map;
this.list = list;
this.dog = dog;
}
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 getHappy() {
return happy;
}
public void setHappy(Boolean happy) {
this.happy = happy;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
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 Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birthday=" + birthday +
", map=" + map +
", list=" + list +
", dog=" + dog +
'}';
}
}
application.yml:
person:
name: 张三
age: 18
happy: true
birthday: 2024/8/1
map: {k1: v1,k2: v2}
list:
- sing
- dance
dog:
name: dog
age: 1
测试类:
package com.demo.helloworld;
import com.demo.helloworld.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
//单元测试
@SpringBootTest
class HelloWorldApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
也可以通过 @PropertySource(value = "classpath:xx.properties") 指定配置文件
//加载指定的配置文件
@PropertySource(value = "classpath:xx.properties")
但需要在属性上方或者 Set 方法上加 @Value 赋值
在 yaml 里可以随便使用占位符或者直接 ${random.uuid} 就能实现随机数