Spring加载yaml列表值

时间:2017-04-11 08:57:43

标签: spring spring-boot

是否有从yaml文件加载特定对象列表的简单方法。所以例如我有yaml文件,配置如下:

list: 
  -
    name: a
    url: a.com
  - 
    name: b
    url: b.com

我想从这个属性创建List<Endpoints>。我知道使用弹簧启动和@ConfigurationProperties注释很容易做到,但我怎么能用弹簧做到这一点?

2 个答案:

答案 0 :(得分:1)

这个怎么样?

yml文件

list: 'a,a.com;b,b.com'

组件类

    @Value("#{T(org.blah.spring.service.Endpoint).getEndpoints('${list}'.split(';'))}")
  List<Endpoint> endpoints;

和终点

@Getter
@Setter
@AllArgsConstructor
public class Endpoint {

  private String name;
  private String url;

  public static List<Endpoint> getEndpoints(List<String> strings){
    List<Endpoint> endpoints = Lists.newArrayList();

    for(String s: strings){
      String split[] = s.split(",");
      endpoints.add(new Endpoint(split[0], split[1]));
    }
    return endpoints;
  }
}

答案 1 :(得分:1)

如果您正在寻找无引导解决方案(如果您正在构建库),请忽略我的答案。

使用PropertiesConfigurationFactoryYamlPropertySourceLoaderMutablePropertySources,您可以将Yaml文件读入Pojo:

import org.junit.Test;
import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.ByteArrayResource;

import java.util.List;

import static org.junit.Assert.assertTrue;

public class YamlTest {

    private static final String YAML_STRING =   "list:          \n" +
                                                "  -            \n" +
                                                "    name: a    \n" +
                                                "    url: a.com \n" +
                                                "  -            \n" +
                                                "    name: b    \n" +
                                                "    url: b.com";

    @Test
    public void shouldLoadYamlIntoObject() throws Exception {
        PropertiesConfigurationFactory<EndpointsHolder> propertiesConfigurationFactory = new PropertiesConfigurationFactory<>(EndpointsHolder.class);

        MutablePropertySources propertySources = new MutablePropertySources();
        YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();

        propertySources.addFirst(yamlPropertySourceLoader.load("list", new ByteArrayResource(YAML_STRING.getBytes()), null));
        propertiesConfigurationFactory.setPropertySources(propertySources);

        EndpointsHolder actual = propertiesConfigurationFactory.getObject();

        assertTrue(actual.getList().get(0).getName().equals("a"));
        assertTrue(actual.getList().get(1).getUrl().equals("b.com"));
    }

    public static class EndpointsHolder {

        List<Endpoints> list;

        public List<Endpoints> getList() {
            return list;
        }

        public void setList(List<Endpoints> list) {
            this.list = list;
        }
    }

    public static class Endpoints {

        String name;
        String url;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}

只需用自己的数据源替换new ByteArrayResource(YAML_STRING.getBytes())