除了注解等方式(参考: Spring Boot获取yml文件配置注入Map/List/String等类型 ),还有一种直接读取的实现:
public static Object readProperties(String key) {
Object propertyValue = null;
Resource resource = new ClassPathResource("config/application-custom.yml");
Properties properties;
try {
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(resource);
properties = yamlFactory.getObject();
propertyValue = properties.get(key);
} catch (Exception e) {
e.printStackTrace();
}
return propertyValue;
}