yaml的使用

作者: 阿帕旗 | 来源:发表于2022-06-11 20:43 被阅读0次

    yaml的使用

    1. 通过yaml给实体类赋值

    先写一个实体类Person

    @Component //把person注册到bean,相当于<bean id="" class="">
    @ConfigurationProperties(prefix = "person") //通过注解配置person
    public class Person {
        private String name;
        private String age;
        private Date birth;
        private Boolean happy;
        private Map<String,String> maps;
        private List<String> list;
        private Dog dog;
    
        public Person() {
        }
    
    
        public Person(String name, String age, Date birth, Boolean happy, Map<String, String> maps, List<String> list, Dog dog) {
            this.name = name;
            this.age = age;
            this.birth = birth;
            this.happy = happy;
            this.maps = maps;
            this.list = list;
            this.dog = dog;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
        }
    
        public Date getBirth() {
            return birth;
        }
    
        public void setBirth(Date birth) {
            this.birth = birth;
        }
    
        public Boolean getHappy() {
            return happy;
        }
    
        public void setHappy(Boolean happy) {
            this.happy = happy;
        }
    
        public Map<String, String> getMaps() {
            return maps;
        }
    
        public void setMaps(Map<String, String> maps) {
            this.maps = maps;
        }
    
        public List<String> getList() {
            return list;
        }
    
        public void setList(List<String> list) {
            this.list = list;
        }
    
        public Dog getDog() {
            return dog;
        }
    
        public void setDog(Dog dog) {
            this.dog = dog;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age='" + age + '\'' +
                    ", birth=" + birth +
                    ", happy=" + happy +
                    ", maps=" + maps +
                    ", list=" + list +
                    ", dog=" + dog +
                    '}';
        }
    }
    

    2.在resources下创建一个application.yaml文件

    在yaml的赋值方式

    yaml的基本语法

    #第一级类名,空格后才是上一级的属性
    #给属性赋值在冒号后面要空格,否则无效,如:name: "李玲"
    Person:
      name: "李玲"
      age: 7
      birth: 2000/7/7
      happy: false
      #Map的写法
      maps: {k1: k1,k2: k2}
      #list数组
      list:
        - code
        - music
        - girl
      Dog:
        name: 旺财
        age: 3
    
    #Dog和Person是平级,以下的Dog不是Person的属性
    Dog:
      name: 小黄
      age: 2
    

    3.测试

    springBoot项目自带的测试方法

    @SpringBootTest
    class SpringDemoApplicationTests {
    
        @Autowired //用注解自动装配
        private Person person;
        @Autowired
        private Dog dog;
        @Test
        void contextLoads() {
    
            System.out.println("person:"+person);
            System.out.println("Dog:"+dog);
        }
    
    }
    

    4.打印结果:

    person:Person{
    name='李玲', 
    age='7', 
    birth=Fri Jul 07 00:00:00 CST 2000, 
    happy=false, maps={k1=k1, k2=k2}, 
    list=[code, music, girl], 
    dog=Dog{name='旺财', age='3'}
    }
    
    Dog:Dog{name='小黄', age='2'}
    

    5.使用yaml要注意配置@ConfigurationProperties(prefix = "xxx")

    以上用log打印Person和Dog类
    必须要配置
    @ConfigurationProperties(prefix = "person")
    @ConfigurationProperties(prefix = "dog")
    否则打印是空值

    6.yaml 和 properties的区别

    yaml的写法:

    #普通的key-value
    name: 李玲
    
    #对象
    student:
      name: liling
      age: 6
    
    #内行写法
    student: {name: liling,age: 6}
    
    #数组
    pets:
      - cat
      - dog
      - pig
    #内行写法
    pets: [cat,dog,pig]
    

    properties的写法:

    name=liling
    student.name=liling
    student.age= 6
    
    person.pets.[0]=cat
    person.pets.[1]=dog
    person.pets.[2]=pig
    

    7.总结:

    yaml通熟易懂,写法简单明了,yaml对空格及其严格,层级关系需要空格来区分,上下左对齐为平级关系。

    相关文章

      网友评论

        本文标题:yaml的使用

        本文链接:https://www.haomeiwen.com/subject/laswmrtx.html