配置yaml语句: 包含对象 数组 Map集合 List数组
# 外对象 person: # 基本属性 name: jsxs age: 18 happy: No birth: 2001/12/01 #键值对 maps: {k1: v1,k2: v2} #数组 lists: - code - music - girls # 内嵌对象 dog: name: 旺财 age: 15
测试
package com.example.springboot01hello; import com.example.springboot01hello.pojo.Dog; import com.example.springboot01hello.pojo.people; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; @SpringBootTest class SpringBoot01HelloApplicationTests { @Resource //进行自动装配的操作,也就是我们前面的 private Dog dog; @Resource private people people; @Test void contextLoads() { System.out.println(dog); System.out.println(people); } }
(3).使用properties进行配置和读取配置属性(SPEL)
package com.example.springboot01hello.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 //javaConfig 绑定我么配置文件的值,可以采用这些方式 // 加载指定的配置文件 @PropertySource(value = "classpath:jsxs.properties") public class people { // SPEL表达式读取配置文件的值 @Value("${name}") private String name; @Value("${age}") private Integer age; @Value("${happy}") private Boolean happy; @Value("${birth}") private Date birth; //这里的日期格式,一定要是xxxx/xx/xx否则报错 private Map<String,Object> maps; private List<Object> lists; private Dog dog; public people(String name, Integer 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 people() { } 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 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 "people{" + "name='" + name + '\'' + ", age=" + age + ", happy=" + happy + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}'; } }
利用properties进行配置
通过EL表达式进行获取数据。${}
(4).yaml的SPEL占位符
${}
# 外对象 person: # 基本属性 name: jsxs${random.uuid} age: ${random.int} happy: false birth: 2001/12/01 hello: sdsdsd #键值对 maps: {k1: v1,k2: v2} #数组 lists: - code - music - girls # 内嵌对象 dog: # 假如说person这个yaml类中不存在hello,那么就会输出hello00_旺财 # 假如说存在: 就会输出存在的hello的值 name: ${person.hello:hello00}_旺财 age: 15
4.yaml与properties的区别
⭐@ConfigurationProperties只需要写一次,value需要每个字段都要加上
⭐松散绑定: 意思就是我的yaml中写的last-name,这个和lastName是一样的,-后面跟着的字母默认是大写的,这就是松散绑定。
⭐JR303数据校验,这个就是我们可以在字段是增加一层过滤器验证,可以保证数据的合法性
。
⭐复杂类型封装,yml中可以封装对象,使用@Value就不支持。
结论
配置yaml和配置properties都可以获取到值 , 但是强烈推荐 yaml
;
如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下 @value;
如果说,我们专门编写了一个JavaBean来和配置文件进行一一映射,就直接@configurationProperties,不要犹豫!