spingboot中yml是我们常用的配置文件,除了spring和其他三方框架支持的配置项之外,我们可以自定义配置项,通过@value来获取简单数据类型的值,同时也可以在yml文件中配置一些复杂数据类型,如对象、集合、map等,闲话少说,直接上代码:
第一步:引入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
第二步:编写一个配置类、申明为配置类或者spring管理的component对象均可、下面罗列了常见的复杂数据类型、其他复杂类型可以根据规则自行扩展。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @title: Cusconfig
* @description: TODO
* @auther: zhangjianyun
* @date: 2022/4/13 9:53
*/
@Component
//@Configuration
@ConfigurationProperties(prefix = "cususer")
public class Cusconfig {
@Value("cusValue")
private String cusValue;
//简单数据类型
private String name;
private Integer age;
private String addr;
//简单list set
private List<String> cuslist;
//简单数组
private String[] cusArray;
//简单map json配置格式
private Map<String,Object> cusmap;
//简单map key-value 格式
private Map<String,Object> cusmap2;
//list map嵌套
private List<Map<String,Object>> cuslist2;
//对象类型 **不能使用内部类对象
private Son son;
@PostConstruct
private void test(){
System.out.println("name="+name);
System.out.println("age="+age);
System.out.println("addr="+addr);
System.out.println("cuslist="+Arrays.toString(cuslist.toArray()));
System.out.println("cusArray="+Arrays.toString(cusArray));
System.out.println("cusmap="+cusmap);
System.out.println("son.age="+son.getAge());
System.out.println("son.name="+son.getName());
System.out.println("cuslist2="+Arrays.toString(cuslist2.toArray()));
System.out.println("cusmap2="+cusmap2);
System.out.println("cusValue="+cusValue);
}
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 String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public List<String> getCuslist() {
return cuslist;
}
public void setCuslist(List<String> cuslist) {
this.cuslist = cuslist;
}
public Map<String, Object> getCusmap() {
return cusmap;
}
public void setCusmap(Map<String, Object> cusmap) {
this.cusmap = cusmap;
}
public Son getSon() {
return son;
}
public void setSon(Son son) {
this.son = son;
}
public Map<String, Object> getCusmap2() {
return cusmap2;
}
public void setCusmap2(Map<String, Object> cusmap2) {
this.cusmap2 = cusmap2;
}
public List<Map<String, Object>> getCuslist2() {
return cuslist2;
}
public void setCuslist2(List<Map<String, Object>> cuslist2) {
this.cuslist2 = cuslist2;
}
public String[] getCusArray() {
return cusArray;
}
public void setCusArray(String[] cusArray) {
this.cusArray = cusArray;
}
public String getCusValue() {
return cusValue;
}
public void setCusValue(String cusValue) {
this.cusValue = cusValue;
}
}
/**
* @title: Son
* @description: TODO
* @auther: zhangjianyun
* @date: 2022/4/13 10:57
*/
public class Son {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
第三步 编写配置文件
cususer:
name: zjy
age: 28
addr: bj
bitrh:
cuslist:
- 'aa'
- 'bb'
- 'cc'
cusArray:
- 'aaa'
- 'bbb'
- 'ccc'
cusmap: {name: zjy,age: 18}
son:
name: zjyson
age: 2
cusmap2:
key1: value1
key2: value2
key3: value3
cuslist2:
- map1:
mapkey1: mapvalue1
mapkey2: mapvalue2
mapkey3: mapvalue3
- map2:
mapkey1: mapvalue111
mapkey2: mapvalue222
mapkey3: mapvalue322
- map3:
mapkey1: mapvalue144
mapkey2: mapvalue244
mapkey3: mapvalue344
cusValue: cusvalue
第四步代码测试 查看spring启动日志
注意事项:
1.所有的属性都应该配置get,set方法否则无法取到配置文件中的值
2.对象类型的参数不能是内部类,只能申明在外部