使用Spring的嵌套数组配置

时间:2019-07-15 10:25:54

标签: spring

我正在尝试从以下yaml中检索配置,其中有一些嵌套数组

ems:
  filtered-queue:
    - filter-regular-expressions:
       - AAA*MD1
       - AAA*MD2
      destination-queue-names:
       - ems.omie1
       - ems.aws1   
    - filter-regular-expressions:
       - BBB*MD1
       - BBB*MD2
      destination-queue-names:
       - ems.omie2
       - ems.aws2

我已经仔细检查过,没有缩进问题。 ConfigServer正在正确读取文件。

我当前检索配置的代码如下

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix="ems")
public class FilteredQueueConfiguration {
    @NestedConfigurationProperty
    private List<FilteredQueue> filteredQueue = new ArrayList<>();


    @Data
    public class FilteredQueue {
        private List<String> filterRegularExpressions = new ArrayList<>();
        private List<String> destinationQueuenames = new ArrayList<>();
    }   
}

在主类中,我具有@EnableConfigurationProperties(FilteredQueueConfiguration.class)批注

我总是有相同的异常,很多东西都绑定到目标...失败。 我做错了什么线索吗?

1 个答案:

答案 0 :(得分:0)

内部类FilteredQueue应该是static类,请参见Type-safe Configuration Properties

import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix="ems")
public class FilteredQueueConfiguration {
    @NestedConfigurationProperty
    private List<FilteredQueue> filteredQueue = new ArrayList<>();


@Data
public static class FilteredQueue {
    private List<String> filterRegularExpressions = new ArrayList<>();
    private List<String> destinationQueuenames = new ArrayList<>();
     }   
 }