新建的person类

package com.example01.demo01.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//标注Spring管理的Bean,使⽤@Component注解在⼀个类上,表⽰将此类标记为Spring容器中的⼀个Bean。好被测试类用@Autowired获取
@ConfigurationProperties(prefix = "person")//从yaml接受数值,可能会有警告,可以不理,也可以按提示去网站加个依赖,pom.xml加个依赖
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> mps;
    private List<Object> lists;
    private Dog dog;



    public Person() {
    }

    public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> mps, List<Object> lists, Dog dog) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.mps = mps;
        this.lists = lists;
        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 getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map<String, Object> getMps() {
        return mps;
    }

    public void setMps(Map<String, Object> mps) {
        this.mps = mps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    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 +
                ", birth=" + birth +
                ", mps=" + mps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

新建的Dog类

package com.example01.demo01.pojo;

//import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
public class Dog {
//    @Value("旺财")也可以不用yaml直接用@Value把值写进去
    private String name;
//    @Value("3")
    private Integer age;

    public Dog() {
    }

    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
        System.out.println("=====");
    }

    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 +
                '}';
    }
}

yaml:yaml对空格很严格“:”,“-”等后面要加空格,yaml里面可以用很多占位符,灵活应用

person:
name: guanglin表示person.name=guanglin
person:
name: guanglin${random.uuid}//生成随机字符
age: ${random.int}//生成随机数字
happy: false
hello: happy
birth: 2018/12/18
#行内写法
#person: {name: guanglin,age: 18,happy: false,birth: 2018/12/18}
maps: {k1: v1,k2: v2}//键值对要写多少都可以
lists:
- code
- music
- girl
#行内写法
# lists:[code,music,girl]
dog:
name: ${person.hello:hello}_旺财
#当person.hello不存在则输出hello_旺财,存在则输出hello对应的数,happy_旺财
age: 3

测试:

    @Autowired//获取person类中bean
    private Person person;
    @Test
    void contextLoads() {
        System.out.println(person);
    }

 

配置文件能够创建的不同位置。优先级高的覆盖优先级低的
1.file:./config/【优先级最高】
2.file:./【优先级第二】
3.classpath:/config/【类路径下的config,优先级第三】
4.classpath:/【直接在类路径,优先级第四】
【类路径:java或者resources目录】

多个配置文件

 

server:
  port: 8083
spring:
  profiles:
    active: test
#运行application-dev.yaml,yaml文件
#  application不能拼错
---
#---算一个文件,spring.profiles=test文件名
server:
  port: 8084
spring:
  profiles: test在我们这个配置文件中能配置的东西,都存在一个固有规律
xxxAutoConfiguration:默认值(自动配置类;给容器中添加组件)
xxxProperties 和配置文件绑定,(封装配置文件中的相关属性)
我就可以使用自定义的配置
可以通过debug=true来查看,那些自动配置类生效,那些没有生效!
yaml文件》
debug=true》运行,看运行结果
posted on 2022-04-28 17:25  阿霖找BUG  阅读(78)  评论(0)    收藏  举报