Вам нужно использовать зависимость Джексона Ямля.
//pom.xml
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
Затем создайте фабричный класс для загрузки файлов yaml в качестве источников свойств.
//YamlPropertySourceFactory.java
import java.io.IOException;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String s,
EncodedResource encodedResource) throws IOException {
YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean();
bean.setResources(encodedResource.getResource());
return new PropertiesPropertySource(
s != null ? s : encodedResource.getResource().getFilename(),
bean.getObject());
}
}
Затем используйте аннотацию PropertySource
, как это.
@PropertySource(factory = YamlPropertySourceFactory.class, value = "testConfigFile.yml")
public class MyTestProperties {
private String simpleProperty;
private List<Person> complexProperties;