yaml基础语法
说明:语法要求严格!
1、空格不能省略,结束不用的分号
2、以缩进来控制层级关系,只要是左边对齐的数据都是同一个层级的。
3、属性和值有大小区分。
yaml注入配置文件
package com.lmh.springboot02.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;
// 跟application.yaml绑定
// 将配置文件的每一个属性的值映射到该类中user
// prefix = "user" 将配置文件中的user下面的属性一一对应
@Component//注册bean
@ConfigurationProperties(prefix = "user")
public class User {
private String name;
private String age;
private Boolean happy;
private Date birth;
private Map<String,Object>maps;
private List<Object>lists;
private Dog dog;
public User() {
}
public User(String name, String age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.maps = maps;
this.lists = lists;
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 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 "User{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", happy=" + happy +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
package com.lmh.springboot02.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
//注册bean
public class Dog {
@Value("汤圆")
private String name;
@Value("3")
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 +
'}';
}
}
application.yaml文件
user:
name: 咸鱼
age: 20
birth: 2021/11/07
happy: false
maps: {k1: v1,k2: v2}
lists:
code
music
girl
dog:
name: 旺财
age: 3
Springboot测试类
package com.lmh.springboot02;
import com.lmh.springboot02.pojo.Dog;
import com.lmh.springboot02.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot02ApplicationTests {
@Autowired //将User自动注入进来
private User user;
@Test
void contextLoads() {
System.out.println(user);
}
}