yaml语法详解

XML 复制代码
#k=v
#对空格的严格要求十分高
#注入到我们的配置类中
#普通的key=value
name: qinjiang

#对象
student:
 name: qingjiang
 age: 3

#行内写法
student1: {name: qinjiang,age: 3}

#数组
pets:
  - cat
  - dog
  - pig

pet: [cat,dog,pig]

yaml可以给实体类赋值

XML 复制代码
person:
  name: kuangshen
  age: 19
  happy: false
  bitth: 2023/07/23
  maps: {k1: v1,k2: v2}
  lists:
    - sing
    - dance
    - girl
  dog:
    name: 旺财
    age: 19
java 复制代码
package com.kuang.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")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date bitth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    public Person() {
    }

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

    public void setBitth(Date bitth) {
        this.bitth = bitth;
    }

    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 +
                ", bitth=" + bitth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}
java 复制代码
package com.kuang.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
//加载指定配置文件
//javaConfig 绑定我们配置文件的值,可以采取这些方式!
//@PropertySource(value = "classpath:qingjiang.properties")
public class Person {
    //spEL表达式取值 ${key}
   // @Value("${name}")
    private String name;
    private Integer age;
    private Boolean happy;
    private Date bitth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    public Person() {
    }

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

    public void setBitth(Date bitth) {
        this.bitth = bitth;
    }

    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 +
                ", bitth=" + bitth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}
XML 复制代码
name=基哥
java 复制代码
package com.kuang;

import com.kuang.pojo.Dog;
import com.kuang.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;

@SpringBootTest
class Springboot02ConfigApplicationTests {
  //自动装配,先根据容器里的组件类型匹配,在根据id,若没有匹配的,可以用@Qualifier(value="xxx");
    @Autowired
  private Person dog;
    @Test
    void contextLoads() {
        System.out.println(dog);
    }

}
相关推荐
Light Gao4 小时前
MCPHub 路由原理:一个入口如何管理并调用上百个 MCP Server
windows·microsoft
vx-程序开发4 小时前
springboot农产品运输服务平台---附源码75498
java·javascript·spring boot·python·eclipse·django·php
攻城有术4 小时前
专项攻克——spring、springMVC、springBoot、springCloud的启动流程
spring boot·spring·spring cloud
独行侠影a4 小时前
SpringBoot 分布式锁实战:Redisson 解决订单超卖并发问题
spring boot·分布式·后端
郭涤生5 小时前
Windows + Ubuntu 双系统启动故障修复
linux·windows·ubuntu
敲个大西瓜7 小时前
Springboot核心面试题
java·spring boot·后端
码农进化录9 小时前
Java 程序员的 AI 进化论 | LangChain4j 入门,Java 开发者的 AI 应用框架
java·spring boot·openai
看浪的路人11 小时前
第3讲:代码补全引擎
开发语言·windows·python
进击切图仔12 小时前
在 windows 和 ubuntu 中配置 host 文件
linux·windows·ubuntu
Boop_wu13 小时前
SpringBoot 集成阿里云短信认证服务(个人)
spring boot·后端·阿里云