springboot读取yml中的数组
钢翼
编程
springboot是支持yml配置数组的,但是直接用@Value是无法读取的。
假设我们要读取的配置如下
xxx:
yy:
- ssssss
- gggggg
错误的写法
@Data
@Component
public class A{
@Value("xxx.yy")
private List<String> yy = new ArrayList<>();
}
正确的写法
@Data
@Component
@ConfigurationProperties(prefix = "xxx")
public class A{
private List<String> yy = new ArrayList<>();
}
所有我们也要避免将数组直接定义在根节点,因为用不了前缀也就无法直接注入。