数组注入
yaml 也支持数组注入,例如
my:
servers:
-
dev.example.com
-
another.example.com
这段数据可以绑定到一个带 Bean 的数组中:
@ConfigurationProperties(prefix=“my”)
@Component
public class Config {
private List servers = new ArrayList();
public List getServers() {
return this.servers;
}
}
项目启动后,配置中的数组会自动存储到 servers 集合中。当然,yaml 不仅可以存储这种简单数据,也可以在集合中存储对象。例如下面这种:
redis:
redisConfigs:
- host: 192.168.66.128
port: 6379
- host: 192.168.66.129
port: 6380
这个可以被注入到如下类中:
@Component
@ConfigurationProperties(prefix = “redis”)
public class RedisCluster {
private List redisConfigs;
//省略getter/setter
}
优缺点
不同于 properties 文件的无序,