application.yml
wm:
test-str: 9527
test-map: {one: '1', two: '2'}
test-list:
- one
- two
- three
- 孙悟空
YmlTestConfig.java
package com.wm.work.database.vo;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Data
@ConfigurationProperties(prefix = "wm")
public class YmlTestConfig implements InitializingBean {
private MapVO testMap;
private List<String> testList;
private String testStr;
public static MapVO testMapVO;
public static List<String> testStrList;
@Override
public void afterPropertiesSet() throws Exception {
YmlTestConfig.testMapVO = testMap;
YmlTestConfig.testStrList = testList;
}
@Data
public static class MapVO {
private String one;
private String two;
}
}
TEST.JAVA
package com.wm.work.database;
import com.wm.work.database.vo.YmlTestConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class WmWorkDatabaseApplicationTests {
@Autowired
private YmlTestConfig ymlTestConfig;
@Test
void getValueList() {
System.out.println(ymlTestConfig.getTestList());
System.out.println(ymlTestConfig.getTestMap());
}
@Test
void getValueConfig() {
System.out.println(YmlTestConfig.testMapVO);
System.out.println(YmlTestConfig.testStrList);
}
}
getValueConfig 打印输出