yaml的使用
1.将原先工程的核心配置文件application.properties修改为application.yaml。名字不能修改,默认是application
2.创建实体类Dog和Person
package com.liuqing.pojo;
import org.springframework.stereotype.Component;
//注入组件
@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 +
'}';
}
}
package com.liuqing.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
//与yaml关联,实现属性注入
@ConfigurationProperties(prefix = "person")
//加载指定的配置文件
//@PropertySource(value ="classth:xxx.properties")
public class Person {
//SPEL表达式获取配置文件的值
// @Value("${name}")
private String name ;
private String age ;
private String happy;
private Date birth;
private List<Object> list;
private Map<String,Object> map;
private Dog dog;
public Person() {
}
public Person(String name, String age, String happy, Date birth, List<Object> list, Map<String, Object> map, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.list = list;
this.map = map;
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 String getHappy() {
return happy;
}
public void setHappy(String 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 Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
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 +
", list=" + list +
", map=" + map +
", dog=" + dog +
'}';
}
}
3.编写yaml
【1】.格式:
后面必须有空格
【2】.对象
person1和person2。
person2:
aaa: a1
bbb: b2
person1:
aaa: a1
bbb: b2
【3】.数组
list:
- code
- music
- gril
【4】.集合
map: {k1: v1,k2: v2}
【5】给对应着实体类Person的属性赋值。
#对象person
person:
#属性name,age,happy...
name: liuqing
#随机站位符
age: ${random.int}
happy: yes
birth: 2020/11/02
#集合list
list:
- code
- music
- gril
#集合map
map: {k1: v1,k2: v2}
#对象dog
dog:
name: 旺财
age: 3
4。测试
@SpringBootTest
class Spring02configApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}