欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot yaml配置文件注入

程序员文章站 2022-05-01 23:08:02
...

Yaml语法

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

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

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

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

K: V

注意:

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

    比如 :name: “123 \n 456” 输出 :123 换行 456

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

    比如 :name: ‘123\n 456’ 输出 :123 \n 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]

导入依赖

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

注入

  1. 新建pojp person dao 类

    package com.qiyu.springbootshiro.pojo;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /**
     * @program: spring-boot-shiro
     * @description:
     * @author: cxy
     * @create: 2021-08-03 14:19
     **/
    //注册bean
    @Component
    public class Dog
    {
        //设置默认值
        @Value("旺财1")
        private  String name;
        @Value("1")
        private Integer 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 +
                    '}';
        }
    }
    
    package com.qiyu.springbootshiro.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;
    
    /**
     * @program: spring-boot-shiro
     * @description:
     * @author: cxy
     * @create: 2021-08-03 14:13
     **/
    //注册bean
    @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;
    
        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 +
                    '}';
        }
    }
    
  2. yaml参数设置

     name: cxy
      age: 20
      happy: false
      birth: 1992/12/10
      maps: {key1: value1,key2: value2}
      lists:
        - 篮球
        - 旅游
        - 板砖
      dog:
        name: 黑豆
        age: 5
    
  3. 测试

    package com.qiyu.springbootshiro;
    
    import com.qiyu.springbootshiro.pojo.Dog;
    import com.qiyu.springbootshiro.pojo.Person;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class SpringBootShiroApplicationTests {
    	//自动注入
        @Autowired
        Person person;
    
        @Autowired
        Dog dog;
    
        @Test
        void contextLoads() {
            System.out.println(person);
    
            System.out.println(dog);
        }
    
    }
    

配置文件占位符

person:    
	name: cxy${random.uuid} # 随机uuid
   	age: ${random.int}  # 随机int
    happy: false
    birth: 1992/12/10
    maps: {k1: v1,k2: v2}
    lists:
    	- 篮球
        - 旅游
        - 板砖
      dog:
      	name: ${person.hello:other}_黑豆
        age: 1
相关标签: java java