SpringBoot yaml配置注入

yaml配置注入

yaml, 也有人写为yml, 读音(鸭买哦) 官方格式为yaml

YAML(/ˈjæməl/,尾音类似camel骆驼)是一个可读性高,用来表达数据序列化的格式。YAML参考了其他多种语言,包括:C语言、Python、Perl,并从XML、电子邮件的数据格式(RFC 2822)中获得灵感。Clark Evans在2001年首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者。当前已经有数种编程语言或脚本语言支持(或者说解析)这种语言。这种语言以数据作为中心,而不是以标记语言为重点!

配置文件优先级properties>yaml>yml (文件名均为application)

在springboot中, 官方推荐使用yaml格式的配置文件来取代properties文件进行配置

  • properties文件格式: key=value

  • yml文件格式: key:空格value

  • 他们的注释都是 #

配置文件可以用来修改springboot中已经配置好的默认值

和xml对比, 例如: (修改服务器端口号)

xml:

<server>
    <port>8081<port>
</server>

yaml:

server:
  prot: 8080

可见语法比xml更加简洁, 更加直观

yaml基础语法

语法要求非常严格, 尤其是缩进, 规则如下:

  1. 空格不能省略
  2. 以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的。
  3. 属性和值的大小写都是十分敏感的。

字面量: 普通值(数组, 布尔, 字符串)

字面量可以直接写而不需要加双引号

K: V

注意:

  • “ ” 双引号,会转义字符串里面的特殊字符 , 特殊字符会作为本身想表示的意思;

    比如 :name: "123 456" 输出 :123 换行 456

  • '' 单引号,不会转义特殊字符 , 特殊字符最终会变成和普通字符一样输出

    比如 :name: ‘123 456’ 输出 :123 456

对象, Map类型 (键值对)

对象和Map的格式有如下两种方式, 写法一致

o: 
    k1: v1
    k2: v2

第一行o是对象名或者map的变量名, 如果下面的k1, k2想和对象或者map建立关系, 必须有缩进

例如: student对象

student:
    name: 张三
    age: 3

集合: 例如Map map = new HashMap();

map:
	k1: v1
	k2: v2

即最终map中会出现两个键值对

当然, 对象和map集合还有一种行内的写法, 如下:

student: {name: zhangsan, age: 3}
map: {k1: v1, k2: v2}

数组 (list and set)

- (减号)值表示数组中的一个元素,比如:

pets:
 - cat
 - dog
 - pig

行内写法:

pets: [cat,dog,pig]

yaml注入案例

1.新建person类:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "person")
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;
}

@Component 注解是把person对象交给spring管理

@ConfigurationProperties(prefix = "person") 次注解是注入配置文件(application.properties)中的对象, prefix为指定的对象

@ConfigurationProperties作用:
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定参数 
prefix = “person” : 将配置文件中的person下面的所有属性一一对应

2. yaml文件内容:

person:
  name: 张pk
  age: 21
  happy: false
  birth: 1999/01/01
  maps: {k1: v1, k2: v2}
  lists:
    - code
    - girl
    - music
  dog:
    name: 旺财
    age: 5

3. 出现提示:

IDEA 提示,springboot配置注解处理器没有找到,让我们看文档,我们可以查看文档,找到一个依赖!

根据提示, 打开对象的网站, 上面说需要导入一个依赖

<!-- 导入配置文件处理器,配置文件进行绑定就会有提示,需要重启 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>

4. 测试

@SpringBootTest
public class MyTest2 {
	@Autowired
    private Person person;

    @Test
    void test2() {
        System.out.println(person);
    }
}

结果:

Person(name=张pk, age=21, happy=false, birth=Fri Jan 01 00:00:00 CST 1999, maps={k1=v1, k2=v2}, lists=[code, girl, music], dog=Dog(name=旺财, age=5))

正常输出

加载指定配置文件

@PropertySource :加载指定的配置文件;

@configurationProperties:默认从全局配置文件(application)中获取值;

读取其他yaml文件

读取其他文件 (yaml)

1. 新建配置文件

我们去在resources目录下新建一个student.yaml文件

student:
  name: 李四
  age: 45
  sex: 男

2. 加载配置文件(student类)

@Data
@AllArgsConstructor
@NoArgsConstructor
@PropertySource("classpath:student.yaml")
@Component
public class Student {
    @Value("${name}")
    private String name;
    @Value("${age}")
    private Integer age;
    @Value("${sex}")
    private String sex; // 性别
}

测试并结果正常输出

读取其他properties文件

读取其他文件 (properties)

1. 新建配置文件

student.properties文件

student.name=李四
student.age=45
student.sex=男

2. 加载配置文件(student类)

@Data
@AllArgsConstructor
@NoArgsConstructor
@PropertySource("classpath:student.properties")
@Component
public class Student {
    @Value("${student.name}")
    private String name;
    @Value("${student.age}")
    private Integer age;
    @Value("${student.sex}")
    private String sex; // 性别
}

读取其他文件对比

读取yaml的时候不需要写对象名字student

读取properties的时候必须写全key的名字才可以

配置文件占位符

例如:

person:
	...
	age: 21
	...

son:
    uuid: UUID:${random.uuid}  # 随机的uuid
    randomInt: ${random.int(8)}  #随机数, 最大值为8, 也可以写${random.int}
    randomLong: ${random.long(3)}  # 随机long值, 也可以写${random.long}
    other: ${person.age:other}_其他值  # 如果有person的age对象, 结果就是[对象值+_其他值], 如果没有就是[other_其他值]

Son类:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "son")
public class Son {
    private String uuid;
    private Integer randomInt;
    private Long randomLong;
    private String other;
}

结果:

Son(uuid=UUID:518d1151-4154-479b-926f-040b86a00cf3, randomInt=6, randomLong=2.0, other=21_其他值)
原文地址:https://www.cnblogs.com/zpKang/p/13232146.html