SpringBoot配置的文件名是固定的:application.yml application.properties
YAML:以数据为中心 比Json xml更适合做配置文件

YAML语法:
1 字面量:普通值(字符串 布尔值 数字)
(1) k: v
(2) " "不会转义 ' '会转义
2 对象,map(属性和值)
(1) k: v
(2) 在下一行 写对象的属性和值

数组(List,set)
用 - 表示数组中的一个元素

行内写法

 
 
properties配置文件容易乱码 需要在设置中找到这个然后调成这样

示例1(在yml文件中配置):
application.yaml文件中配置
Person:
  name: 张三
  age: 21
  boss: true
  birth: 2022/12/5
  maps: {key1: value1,key2: value2}
  list: [dog,cat,pig]Person类中利用ConfigurationProperties(prefix=" ")引入配置类中的元素 但是注意Person类中的类的名字必须与application.yaml中一致
package com.qcby.model;
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 = "person")
public class Person {
    private String name;
    private Integer age;
    private boolean boss;
    private Date birth;
    private Map<String,String> maps;
    private List<String> list;
    public Person() {
    }
    public Person(String name, Integer age, boolean boss, Date birth, Map<String, String> maps, List<String> list) {
        this.name = name;
        this.age = age;
        this.boss = boss;
        this.birth = birth;
        this.maps = maps;
        this.list = list;
    }
    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 isBoss() {
        return boss;
    }
    public void setBoss(boolean boss) {
        this.boss = boss;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public Map<String, String> getMaps() {
        return maps;
    }
    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", list=" + list +
                '}';
    }
}
最终在Test包下进行测试
@RunWith(SpringRunner.class)  //声明为测试单元
@SpringBootTest  //读取配置
public class SpringBoot01ApplicationTests {
    @Autowired
    Person person;
   
    @Test
    public void Context(){
        System.out.println(person);
    }
    }示例2(在properties文件中配置)
在application.properties中
person.email=lucky
person.hello=junit
person.lastName=jiangjiayi
person.age=12
person.boss=true
person.list=dog,cat
person.maps.key1=value1
person.maps.key2=value2
person.dog.name=${person.hello}  //${} 可以理解为Vue中的{{}} 调用的是上面的person.hello的值
person.dog.age=16Person类
package com.qcby.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    String email;
    String hello;
    String lastName;
    int age;
    boolean boss;
    List<String> list;
    Map<String,String>maps;
    Dog dog;
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getHello() {
        return hello;
    }
    public void setHello(String hello) {
        this.hello = hello;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public boolean isBoss() {
        return boss;
    }
    public void setBoss(boolean boss) {
        this.boss = boss;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public Map<String, String> getMaps() {
        return maps;
    }
    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "Person{" +
                "email='" + email + '\'' +
                ", hello='" + hello + '\'' +
                ", lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", list=" + list +
                ", maps=" + maps +
                ", dog=" + dog +
                '}';
    }
}
Dog类
package com.qcby.model;
public class Dog {
    String name;
    int age;
    public Dog() {
    }
    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    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;
    }
}
测试类
@RunWith(SpringRunner.class)  //声明为测试单元
@SpringBootTest  //读取配置
public class SpringBoot01ApplicationTests {
    @Autowired
    Person person;
   
    @Test
    public void Context(){
        System.out.println(person);
    }
    }如果不是标准的application.properties 是其他的Person类中需要用PropertySource来添加配置文件的路径
@ImportSource
导入Spring的配置文件,让配置文件里面的内容生效;Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上

SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式
1、配置类@Configuration------>Spring配置文件
2、使用@Bean给容器中添加组件,将方法的返回值添加到容器中 容器中这个组件的默认ID就是方法名
生成随机数:
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
多profile文件
通过spring:
profiles:
active: 来决定运行谁

配置文件加载位置的优先级
–file:./config/
 –file:./
 –classpath:/config/
 –classpath:/
优先级由高到底,高优先级的配置会覆盖低优先级的配置; 










