spring boot陣列型屬性的配置
阿新 • • 發佈:2018-12-19
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