Spring Boot从application.yml注入映射列表



我试图从Spring Boot配置注入映射列表,但得到了一个空列表。如何正确注入?

cacheConfigs:
- cacheOne:
test: test1
- cacheTwo:
test: test2
- cacheThree:
test: test3
- cacheFor:
test: test4
@ConfigurationProperties(prefix = "cacheConfigs")
public List<Map<String, String>> getCacheConfigs() {
return new ArrayList<>();
}

这是一个;新的";对我来说。我通过将cacheConfigs更深一层来实现这一点,并使用新的顶级名称作为@ConfigurationProperties参数。像这样:

cache-configs-map:
cacheConfigs: 
- cacheOne:
test: test1
- cacheTwo:
test: test2
- cacheThree:
test: test3
- cacheFor:
test: test4

现在,您的配置类如下:

@Configuration
public class Config{
@NoArgsConstructor @AllArgsConstructor( staticName = "of" )
@Getter @Setter
public static class C{
private List<Map<String, String>> cacheConfigs;
}

@Bean
@ConfigurationProperties(prefix = "cache-configs-map")
public C getC() {
return new C();
}
}

最新更新