假设我们在classpath
上有一个yml
属性配置文件application.yml
,我们想将它程序化读取到Properties
对象,可以使用YamlPropertiesFactoryBean
,如下所示 :
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("application.yml"));
Properties properties=yaml.getObject();
如果application.yml
内容如下 :
key :
key01 : primary
key02 : slave
path : ${key.key01}/foo
则properties
对象内容如下 :
key.key01 = primary
key.key02 = slave
path = ${key.key01}/foo
需要注意的是,path
属性值中的${key.key01}
并不会被解析。