spring boot普通属性配置非常方便 通过@value(${属性名称})就可以绑定到相关的属性值.但是对于某些比较复杂的对象属性配置且改对象数目不确定时通过固定的属性名配置显然有太复杂,通过list结合map可以较为灵活的对这类对象进行配置范例如下:
@Configuration
@ConfigurationProperties(prefix="com.all.security")
public class ClientConfig {
private List<Map<String,String>> client;
public List<Map<String, String>> getClient() {
return client;
}
public void setClient(List<Map<String, String>> client) {
this.client = client;
}
}
要获取属性值时通过map
map.get("clientId")
// 对应的key值即可
#client config
com.all.security.client[0].clientId=client_1
#
com.all.security.client[0].secret=client_1
com.all.security.client[0].accessTokenValiditySeconds=36000
com.all.security.client[0].resourceIds=dataaccess
com.all.security.client[0].authorizedGrantTypes=client_credentials,refresh_token
com.all.security.client[0].scopes=select
#com.all.security.client[1].clientId]=client_2
#
#com.all.security.client[1].secret=client_2
#com.all.security.client[1].accessTokenValiditySeconds=36000
#com.all.security.client[1].resourceIds=order
#com.all.security.client[1].authorizedGrantTypes=client_credentials,refresh_token
#com.all.security.client[1].scopes=select