SpringBoot——yml配置文件的书写和读取

之前一直用的application.properties配置文件,只能是KV结构,后来的yml配置文件更像是树状结构,支持层级,比properties更灵活;

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:

/**
 * 2022年10月25日下午5:57:43
 */
package testspringboot.test3;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author XWF
 *
 */
@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 + "]";
	}
	
}
/**
 * 2022年10月27日上午9:45:35
 */
package testspringboot.test3;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author XWF
 *
 */
@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:

/**
 * 2022年10月26日上午10:06:10
 */
package testspringboot.test3;

import java.util.List;
import java.util.Set;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author XWF
 *
 */
@Component
@ConfigurationProperties(prefix = "")
public class MyList {

	public List myArrayA;
	public Set myArrayB;
	public Integer[] myListC;
	public List myListD;
	public List myListE;
	public List myListF;
	public List> myListG;

	public List getMyArrayA() {
		return myArrayA;
	}
	public void setMyArrayA(List myArrayA) {
		this.myArrayA = myArrayA;
	}
	public Set getMyArrayB() {
		return myArrayB;
	}
	public void setMyArrayB(Set myArrayB) {
		this.myArrayB = myArrayB;
	}
	public Integer[] getMyListC() {
		return myListC;
	}
	public void setMyListC(Integer[] myListC) {
		this.myListC = myListC;
	}
	public List getMyListD() {
		return myListD;
	}
	public void setMyListD(List myListD) {
		this.myListD = myListD;
	}
	public List getMyListE() {
		return myListE;
	}
	public void setMyListE(List myListE) {
		this.myListE = myListE;
	}
	public List getMyListF() {
		return myListF;
	}
	public void setMyListF(List myListF) {
		this.myListF = myListF;
	}
	public List> getMyListG() {
		return myListG;
	}
	public void setMyListG(List> 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:

/**
 * 2022年10月26日下午4:35:02
 */
package testspringboot.test3;

import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author XWF
 *
 */
@Component
@ConfigurationProperties(prefix = "maps")
public class MyMap {

	public Map mapA;
	public Map mapB;
	public Map mapC;
	public Map> mapD;
	public Map> mapE;
	public Map mapF;

	public Map getMapA() {
		return mapA;
	}
	public void setMapA(Map mapA) {
		this.mapA = mapA;
	}
	public Map getMapB() {
		return mapB;
	}
	public void setMapB(Map mapB) {
		this.mapB = mapB;
	}
	public Map getMapC() {
		return mapC;
	}
	public void setMapC(Map mapC) {
		this.mapC = mapC;
	}
	public Map> getMapD() {
		return mapD;
	}
	public void setMapD(Map> mapD) {
		this.mapD = mapD;
	}
	public Map> getMapE() {
		return mapE;
	}
	public void setMapE(Map> mapE) {
		this.mapE = mapE;
	}
	public Map getMapF() {
		return mapF;
	}
	public void setMapF(Map 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:

/**
 * 2022年10月25日下午4:02:58
 */
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;

/**
 * @author XWF
 *
 */
@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 list;
	@Value("${myListC}")
	public Set 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服务):

/**
 * 2022年10月25日下午2:53:50
 */
package testspringboot.test3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author XWF
 *
 */
@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配置文件的书写和读取_第1张图片

你可能感兴趣的:(SpringBoot,spring,boot,java,yml)