SpringBoot之----yaml语法详解

SpringBoot之----yaml语法详解,第1张

SpringBoot之----yaml语法详解

1、SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的

application.properties
语法结构 :key=value
application.yml
语法结构 :key:空格 value

2、什么是yaml
这种语言以数据作为中心,而不是以标记语言为重点!
传统xml配置:


    8081

yaml配置:

server:
  prot: 8080

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

# 字面量:普通的值  [ 数字,布尔值,字符串  ]
# 字面量直接写在后面就可以 , 字符串默认不用加上双引号或者单引号;
k: v

注意:
“ ” 双引号,不会转义字符串里面的特殊字符 , 特殊字符会作为本身想表示的意思;
比如 :name: “xianyu n fanshen” 输出 :xianyu 换行 fanshen
’ ’ 单引号,会转义特殊字符 , 特殊字符最终会变成和普通字符一样输出
比如 :name: ‘xianyu n fanshen’ 输出 :xianyu n fanshen
4、对象、Map(键值对)
一般格式:

k: 
    v1:
    v2:

注意缩进:

student:
    name: xianyu
    age: 3

行内写法:

student: {name: xianyu,age: 18}

5、数组( List、set )
-值表示:

pets:
 - cat
 - dog
 - pig

行内写法:

pets: [cat,dog,pig]

修改默认端口:

server:
  port: 8082

6、yaml注入配置文件
在springboot项目中的resources目录下新建一个文件 application.yml
编写一个实体类 Dog;原来是如何给bean注入属性值的?

package com.wlm.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Dog {
    @Value("老黑")
    private String name;
    @Value("18")
    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 +
                '}';
    }
}

在SpringBoot的测试类下注入狗狗输出一下;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

    @Autowired //将狗狗自动注入进来
            Dog dog;

    @Test
    public void contextLoads() {
        System.out.println(dog); //打印看下狗狗对象
    }

}

输出结果:

编写一个实体类Person:

@Component //注册bean到容器中
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map maps;
    private List lists;
    private Dog dog;
    
    //有参无参构造、get、set方法、toString()方法  
}
 

我们来使用yaml配置的方式进行注入,我们编写一个yaml配置:
application.yml

person:
  name: 咸鱼_翻身
  age: 18
  happy: true
  birth: 2000/11/26
  maps: {k1: one,k2: two}
  lists:
    - ball
    - girl
    - music
  dog:
    name: 旺财
    age: 3

我们刚才已经把person这个对象的所有值都写好了,我们现在来注入到我们的类中:
Person

@Component //注册bean
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map maps;
    private List lists;
    private Dog dog;
}
 

@ConfigurationProperties作用:
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应
确认以上配置都OK之后,我们去测试类中测试一下:

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    Person person; //将person自动注入进来

    @Test
    public void contextLoads() {
        System.out.println(person); //打印person信息
    }

}

运行结果:所有值全部注入成功!

Person{name='咸鱼_翻身', age=18, happy=true, 
birth=Sun Nov 26 00:00:00 CST 2000, maps={k1=one, k2=two}, 
lists=[ball, girl, music], dog=Dog{name='旺财', age=3}}

注意:
1、将配置文件的key 值 和 属性的值设置为不一样,则结果输出为null,注入失败
2、在配置一个person2,然后将 @ConfigurationProperties(prefix = “person2”) 指向我们的person2;
3、依赖需要多导一个----配置文件处理器

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5596604.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)