yaml语法讲解
YAML 是一个可读性高,用来表达数据序列化的格式。YAML也参考了其他的各种语言,包括:
C语言
、Python
等,并从XML
的数据格式中获得灵感。从而创造出的语言格式
Y A M L 语 法 \color{#0000FF}{YAML语法} YAML语法
# K是属性名 v是属性值
k: v
Y A M L 表 示 对 象 \color{#FF7D00}{YAML表示对象} YAML表示对象
people:
name:kisa
age:3
# 行内写法
people: {name:kisa,age:3}
Y A M L 表 示 数 组 \color{#FF7D00}{YAML表示数组} YAML表示数组
pets:
-cat,
-dog
# 行内写法
pets: [cat,dog]
Y A M L 给 属 性 赋 值 \color{#0000FF}{YAML给属性赋值} YAML给属性赋值
代 码 实 例 \color{#0000FF}{代码实例} 代码实例
创建Dog实体类
package com.j.po;
import org.springframework.stereotype.Component;
/**
* Created by jdx on 2022/3/17 上午10:47
*/
@Component
public class Dog {
private String name;
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 +
'}';
}
}
创建People实体类
package com.j.po;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Created by jdx on 2022/3/17 上午10:47
*/
//标注一个类为Spring容器的Bean,(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
@Component
//引用yaml文件并set值
@ConfigurationProperties(prefix = "people")
public class People {
private String name;
private Integer age;
private Boolean happy;
private Date birth;
private List<Object> list;
private Dog dog;
private Map<String, Object> map;
/*无参构造*/
public People() {
}
/*有参构造*/
public People(String name, Integer age, Boolean happy, Date birth, List<Object> list, Dog dog, Map<String, Object> map) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.list = list;
this.dog = dog;
this.map = map;
}
/*get set方法*/
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 List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
/*toString方法*/
@Override
public String toString() {
return "people{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birth=" + birth +
", list=" + list +
", dog=" + dog +
", map=" + map +
'}';
}
}
解 决 S p r i n g B o o t C o n f i g u r a t i o n A n n o t a t i o n P r o c e s s o r n o t c o n f i g u r e d \color{#FF00FF}{解决Spring Boot Configuration Annotation Processor not configured} 解决SpringBootConfigurationAnnotationProcessornotconfigured
<!--解决不影响运行的报错依赖:Spring Boot Configuration Annotation Processor not configured-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
创建set值yaml文件
people:
name: MJl
age: 18
happy: true
birth: 1999/01/11
list:
- code
- music
- girl
dog:
name: 来福
age: 3
创建ApplicationTests测试类
package com.j;
import com.j.po.People;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
class ApplicationTests {
@Resource
private People people;
@Test
void contextLoads() {
System.out.println(people);
}
}
运 行 结 果 \color{#FF00FF}{运行结果} 运行结果
people{name='MJl', age=18, happy=true, birth=Mon Jan 11 00:00:00 CST 1999, list=[code, music, girl], dog=Dog{name='来福', age=3}, map=null}
以上代码实现逻辑来看yaml是可以作为set注入的一种方式