2. 解析yaml文件-YamlPropertiesFactoryBean、YamlMapFactoryBean
文档入口
文档: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.yaml.mapping-to-properties
概述
代码
test.yml
application:
file-name: application.yml
spring:
profiles:
active: test,test2,none
my:
num: ${random.long}
num2: ${random.int(100,200)}
sports:
- swim
- run
test2.yml
test2:
name: test2
version: 1.0
OtherTest.java
@SpringBootTest
public class OtherTest {
@Test
@SneakyThrows
void contextLoads9() {
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
// yamlPropertiesFactoryBean.setResources(new FileUrlResource("E:\\IDEA_Project\\Prosonal\\spirngboot226\\src\\main\\resources\\test.yml"));
yamlPropertiesFactoryBean.setResources(new ClassPathResource("test.yml"),new ClassPathResource("test2.yml"));
Properties properties = yamlPropertiesFactoryBean.getObject();
Console.log(properties);
YamlMapFactoryBean yamlMapFactoryBean = new YamlMapFactoryBean();
// yamlPropertiesFactoryBean.setResources(new FileUrlResource("E:\\IDEA_Project\\Prosonal\\spirngboot226\\src\\main\\resources\\test.yml"));
yamlMapFactoryBean.setResources(new ClassPathResource("test.yml"),new ClassPathResource("test2.yml"));
Map<String, Object> map = yamlMapFactoryBean.getObject();
Console.log(map);
}
}