yml文件中的Spring Load Map值

时间:2019-07-30 09:47:01

标签: java spring-boot

我有一个带有以下application.yml的Spring Boot应用程序

Detail_1:
  name: X,Y,Z
  place: xplace,yplace,zplace
Detail_2:
  name: X,Y,Z
  place: xplaceanother,yplaceanother,zplaceanother

如何在Java中获取此地图:

X {
detail1 :xplace
detail2 :xplaceanother
 }
Y {
detail1:yplace,
detail2:yplaceanother
}
Z{
detail1:zplace,
detail2:zplaceanother
}

我尝试了以下代码:

@Value${detail1.name}
private String names;
@value${detail2.place}
List<Object> Names = Arrays.asList(getNames().split(",")); 
List<Object> places = Arrays.asList(getPlaces().split(","));

然后我尝试创建对应于详细信息1的名称和地点的地图 同样,我获取了详细的名称和位置2 但是在这种情况下,我最终得到了2张地图,一张用于细节1,一张用于细节2。 我需要创建一张地图。

3 个答案:

答案 0 :(得分:0)

您需要使用@ConfigurationProperties批注

以下URL以.properties.yml格式提供了很好的示例:

https://www.mkyong.com/spring-boot/spring-boot-configurationproperties-example/

https://www.baeldung.com/configuration-properties-in-spring-boot

答案 1 :(得分:0)

请像下面application.yml

中那样更新您的配置
map:
    detail1:
      name:X,Y,Z
      place:xplace,yplace,zplace
    detail2:
      name:X,Y,Z
      place:xplaceanother,yplaceanother,zplaceanother

,然后按如下所示配置属性,

DetailConfig.java

@Component
@ConfigurationProperties(prefix="map")
public class DetailConfig {

    private Map<String, Object> detail1;

    private Map<String, Object> detail2;

    public Map<String, Object> getDetail1() {
        return detail1;
    }

    public void setDetail1(Map<String, Object> detail1) {
        this.detail1 = detail1;
    }

    public Map<String, Object> getDetail2() {
        return detail2;
    }

    public void setDetail2(Map<String, Object> detail2) {
        this.detail2 = detail2;
    }
}

答案 2 :(得分:0)

您可以将以下pojo用于属性;

public class Detail {

    private List<String> name;
    private List<String> place;

    public Map<String, String> getNamePlaceMap() {
        return IntStream.range(0, name.size()).boxed()
                .collect(Collectors.toMap(i -> name.get(i), i -> place.get(i)));
    }

    // getters/setters
}

并使用以下配置使属性进入上下文;

@Configuration
public class Config {

    @Bean
    @ConfigurationProperties(prefix = "detail-1")
    public Detail detailOne() {
        return new Detail();
    }

    @Bean
    @ConfigurationProperties(prefix = "detail-2")
    public Detail detailTwo() {
        return new Detail();
    }
}

并将它们自动连线,并将它们传递给创建该映射的逻辑;

@Service
public class TestService {

    @Autowired
    private Detail detailOne;

    @Autowired
    private Detail detailTwo;

    public void test() {
        System.out.println(createSpecialMap(detailOne, detailTwo));
    }

    private static Map<String, Map<String, String>> createSpecialMap(Detail detailOne, Detail detailTwo) {
        Map<String, Map<String, String>> resultMap = new HashMap<>();
        detailOne.getNamePlaceMap().forEach((key, value) -> {
            Map<String, String> subMap = resultMap.getOrDefault(key, new HashMap<>());
            subMap.put("detail1", value);
            resultMap.put(key, subMap);
        });
        detailTwo.getNamePlaceMap().forEach((key, value) -> {
            Map<String, String> subMap = resultMap.getOrDefault(key, new HashMap<>());
            subMap.put("detail2", value);
            resultMap.put(key, subMap);
        });
        return resultMap;
    }
}

导致;

{
  X={detail1=xplace, detail2=xplaceanother}, 
  Y={detail1=yplace, detail2=yplaceanother}, 
  Z={detail1=zplace, detail2=zplaceanother}
}


或者使用Letter类来提高可读性;

public class Letter {

    private String name;
    private String detail1;
    private String detail2;

    public Letter(String name, String detail1, String detail2) {
        this.name = name;
        this.detail1 = detail1;
        this.detail2 = detail2;
    }

    // getters/setters
}

执行以下操作;

private static List<Letter> createList(Detail detailOne, Detail detailTwo) {
    List<Letter> resultList = new ArrayList<>();
    Map<String, String> detailOneMap = detailOne.getNamePlaceMap();
    Map<String, String> detailTwoMap = detailTwo.getNamePlaceMap();

    Set<String> keySet = new HashSet<>();
    keySet.addAll(detailOneMap.keySet());
    keySet.addAll(detailTwoMap.keySet());

    return keySet.stream()
            .map(key -> new Letter(key, detailOneMap.get(key), detailTwoMap.get(key)))
            .collect(Collectors.toList());
}

将导致;

[
  Letter{name='X', detail1='xplace', detail2='xplaceanother'}, 
  Letter{name='Y', detail1='yplace', detail2='yplaceanother'}, 
  Letter{name='Z', detail1='zplace', detail2='zplaceanother'}
]

比原始地图map更好的结果...