SpringBoot读写操作yml配置文件方法
作者:FlyLikeButterfly
yml配置规则
属性跟属性值之间使用“:”和一个“空格”隔开,层级结构通过缩进对齐,缩进只能使用空格,不能用tab,并且大小写敏感,使用#注释文档;
yml配置除了能像properties读取为kv结构,还能方便的读取成集合(List、Set等)、数组、对象、Map,还能进行嵌套结构读取;
普通的kv读取
普通kv结构可以直接使用@Value标签读取,@Value除了可以读取普通kv,也能读取List或Map结构里的某一项;另外使用Environment.getProperty()方法也能像properties那样读取kv结构,但也只能以kv结构读取;
yml格式:
xxx: xxxx
xxx:
xxx: xxxx
xxx:
xxx: xxxx
xxx: xxxx
xxx: xxxx
可以直接使用@Value("${xxx}")、@Value("${xxx.xxx.xxx}")读取;
读取到集合和数组
使用@Value直接读取好像只能使用xxx: xxxx,xxxx,xxxx逗号隔开的格式,可以读取成List、Set或者数组,其他格式可以放到Bean里当作类的属性读取;
yml格式:
xxx:
- xxxx
- xxxx
- xxxx
xxx: [xxxx,xxxx,xxxx]
xxx: xxxx,xxxx,xxxx
读取为对象和Map
对象和Map的格式是一样的,只是读取的目标不同;读取为对象时需要提供setXXX方法,并且XXX要跟配置文件里命名一致;(另外测试时发现自动生成的set方法,对驼峰结构修改大小写会造成无法识别到配置)
yml格式:
xxx:
x: xxxx
x: xxxx
x: xxxx
xxx: {x: xxxx, x: xxxx, x: xxxx}
另外各种格式可以组合嵌套使用,例如list里使用map或者map里包含list;
Demo:
yml配置文件application.yml:
#yml文件
a: hello
val:
b: 123
c: false
d: ENV
mybeantest:
name: Tom
age: 14
addr: 北京
bestfriend: ${myArrayA[3]}
mybeantest2: {name: Jerry, age: 13, id: 1001}
myArrayA:
- tom
- jerry
- jack
- alice
myArrayB: [a,b,c]
myListC: 3,4,5,6
myListD: [{name: Jerry, age: 11, addr: 上海},{name: Jack, age: 12, addr: 北京}]
myListE:
- {name: Bob, age: 22}
- {name: Lily, age: 21}
myListF:
- name: AAA
age: 11
- name: BBB
age: 22
myListG:
- - aaa
- bbb
- ccc
- - dd
- ee
- ff
maps:
mapA:
A: 1
B: 2
C: 3
D: 4
mapB:
a: AA
b: BB
c: CC
mapC: {X: x, Y: y, Z: z}
mapD:
L1:
- x
- y
- z
L2:
- X
- Y
- Z
mapE:
M1:
a: A
b: B
M2:
C: c
D: d
mapF:
m1: {name: TOM, age: 33}
m2: {name: JERRY, age: 44}
读取为bean的类MyBean.java和MyBean2.java:
package testspringboot.test3; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "mybeantest") public class MyBean { public String name; public int age; public String address; public String bestFriend; 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; } public String getAddress() { return address; } public void setAddr(String address) {//setXXX方法,只需XXX跟配置文件里的名称对应即可(自动生成的set方法驼峰结构修改大小写会造成干扰) this.address = address; } public String getBestFriend() { return bestFriend; } public void setBestFriend(String bestFriend) { this.bestFriend = bestFriend; } @Override public String toString() { return "MyBean [name=" + name + ", age=" + age + ", address=" + address + ", bestFriend=" + bestFriend + "]"; } }
package testspringboot.test3; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "mybeantest2") public class MyBean2 { public String name; public int age; public int id; 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; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "MyBean2 [name=" + name + ", age=" + age + ", id=" + id + "]"; } }
读取为集合的类MyList.java:
package testspringboot.test3; import java.util.List; import java.util.Set; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "") public class MyList { public List<String> myArrayA; public Set<String> myArrayB; public Integer[] myListC; public List<MyBean> myListD; public List<MyBean> myListE; public List<MyBean> myListF; public List<List<String>> myListG; public List<String> getMyArrayA() { return myArrayA; } public void setMyArrayA(List<String> myArrayA) { this.myArrayA = myArrayA; } public Set<String> getMyArrayB() { return myArrayB; } public void setMyArrayB(Set<String> myArrayB) { this.myArrayB = myArrayB; } public Integer[] getMyListC() { return myListC; } public void setMyListC(Integer[] myListC) { this.myListC = myListC; } public List<MyBean> getMyListD() { return myListD; } public void setMyListD(List<MyBean> myListD) { this.myListD = myListD; } public List<MyBean> getMyListE() { return myListE; } public void setMyListE(List<MyBean> myListE) { this.myListE = myListE; } public List<MyBean> getMyListF() { return myListF; } public void setMyListF(List<MyBean> myListF) { this.myListF = myListF; } public List<List<String>> getMyListG() { return myListG; } public void setMyListG(List<List<String>> myListG) { this.myListG = myListG; } @Override public String toString() { return "MyList [myArrayA=" + myArrayA + ",\n myArrayB=" + myArrayB + ",\n myListC=" + myListC + ",\n myListD=" + myListD + ",\n myListE=" + myListE + ",\n myListF=" + myListF + ",\n myListG=" + myListG + "]"; } }
读取为Map的类MyMap.java:
package testspringboot.test3; import java.util.List; import java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "maps") public class MyMap { public Map<String, Integer> mapA; public Map<String, String> mapB; public Map<String, String> mapC; public Map<String, List<String>> mapD; public Map<String, Map<String, String>> mapE; public Map<String, MyBean2> mapF; public Map<String, Integer> getMapA() { return mapA; } public void setMapA(Map<String, Integer> mapA) { this.mapA = mapA; } public Map<String, String> getMapB() { return mapB; } public void setMapB(Map<String, String> mapB) { this.mapB = mapB; } public Map<String, String> getMapC() { return mapC; } public void setMapC(Map<String, String> mapC) { this.mapC = mapC; } public Map<String, List<String>> getMapD() { return mapD; } public void setMapD(Map<String, List<String>> mapD) { this.mapD = mapD; } public Map<String, Map<String, String>> getMapE() { return mapE; } public void setMapE(Map<String, Map<String, String>> mapE) { this.mapE = mapE; } public Map<String, MyBean2> getMapF() { return mapF; } public void setMapF(Map<String, MyBean2> mapF) { this.mapF = mapF; } @Override public String toString() { return String.format("MyMap [mapA=%s,\n mapB=%s,\n mapC=%s,\n mapD=%s,\n mapE=%s,\n mapF=%s]", mapA, mapB, mapC, mapD, mapE, mapF); } }
进行测试用的类Test3Class.java:
package testspringboot.test3; import java.util.List; import java.util.Set; import javax.annotation.PostConstruct; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class Test3Class { @Value("${a}") public String a; @Value("${val.b}") public int b; @Value("${val.c}") public boolean c; @Autowired public Environment env; @Resource public MyBean bean; @Resource public MyBean2 bean2; @Value("${myArrayA[3]}") public String arrayA_3; @Value("${myArrayB[1]}") public String arrayB_1; @Value("${myListC}") public List<Integer> list; @Value("${myListC}") public Set<String> set; @Value("${myListC}") public String[] array; @Resource public MyList mylist; @Resource public MyMap mymap; @PostConstruct public void fun() { System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(env.getProperty("val.d")); System.out.println(bean); System.out.println(bean2); System.out.println(arrayA_3); System.out.println(arrayB_1); System.out.println(list); System.out.println(set); System.out.println(array); System.out.println(mylist); System.out.println(mymap); } }
启动类(只测配置文件,所以设置WebApplicationType.NONE关闭web服务):
package testspringboot.test3; import org.springframework.boot.SpringApplication; import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Test3Main { /** * @param args */ public static void main(String[] args) { SpringApplication sapp = new SpringApplication(Test3Main.class); sapp.setWebApplicationType(WebApplicationType.NONE); sapp.run(args); } }
执行结果:
到此这篇关于SpringBoot读写操作yml配置文件方法的文章就介绍到这了,更多相关SpringBoot读写操作yml 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!