yaml使用

application.yaml


# k=v
#对空格要求很高
#可以注入到我们的配置类中
#yaml可以直接给实体类赋值
#普通的key-value
name: jsp
#对象
student:
  name:jsp
  age:3

#行内写法
student:{name:jsp,age:3}

#数组
pets:
  - cat
  - dog
  - elephent
  - pig

petss: [cat,dog,pig]

application.properties

#properties只能保存键值对
name=jsp

student.name=jsp
student.age=3
@ConfigurationProperties(.yaml) @Value(.properties)
功能 批量注入配置文件的属性 一个个指定
松散绑定(松散语法)lastName和last-name 支持 不支持
SpEL 不支持 支持
JSR303@Validated 数据校验 支持 不支持
复杂类型封装 支持 不支持

jsr303校验文件位置
validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar!\javax\validation\constraints

yaml文件位置生效先后顺序
file/config/application.yaml
file/application.yaml
file/src/resources/config/application.yaml
file/src/resources/application.yaml

多环境切换配置文件

#.properties
#springBoot的多环境配置:可以选择激活哪一个配置文件
spring.profiles.active=test

.yaml

server:
  port: 8081
spring:
  profiles:
    active: dev
---
server:
  port: 8082
spring:
  profiles: dev
---
server:
  port: 8083
spring:
  profiles: test

yaml属性注入

person:
  name: jsp${random.uuid} #随机uuid
  age: ${random.int}  #随机数
  happy: false
  birth: 2021/12/01
  maps: {k1: v1,k2: v2}
  lists:
    - code
    - music
    - girl
  dog:
    name: ${person.hello:hello}_wangcai   #占位符 如果不存在person.hello就是使用hello
    age: 3
package com.ji.pojo;

import org.springframework.beans.factory.annotation.Value;
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")//绑定yaml配置文件
/*将配置文件中配置的每个属性的值,映射到这个组件中
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
参数perfix="person":将配置文件中的person下面的所有属性一一对应

只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能*/

//@PropertySource与@Value(SPEL表达式取出配置文件的值"${???}")联合使用 ***.properties
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    public Person() {
    }

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

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

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

总结

  • 配置yaml和配置properties都可以获得值,但是推荐使用yaml
  • 在某个业务中,只需获得配置文件中的某个值,可以使用@Value
  • 如果我们专门编写了一个javaBean来和配置文件进行映射,直接使用@configureProperties
posted @ 2021-12-01 10:39  一刹流云散  阅读(95)  评论(0编辑  收藏  举报