java - How to define complex list for application yml in spring boot? -
suppose have application.yml
defined:
spring: profiles.active: development --- spring: profiles: development logging: level: org.springframework.web: debug org.hibernate: error storage: remotestorage: name: "server 1" user: "test" passwd: "test" host: "163.218.233.106" localstorage: name: "server 2" user: "test2" passwd: "test2" host: "10.10.0.133" ---
the storage
custom properties want load this:
@value("${storage}") private list<storage> availablestorage = new arraylist<>();
apologies beginner spring framework. trying have restcontroller
read respective config based on parameters provided requests, i.e. requests ?storage=remotestorage
use remotestorage
config.
@value annotation not load list or map type of properties. have define class @configurationproperties annotation below.
import java.util.list; import java.util.map; import org.springframework.boot.context.properties.configurationproperties; import org.springframework.context.annotation.configuration; @configuration @configurationproperties(prefix ="storage") public class myprops { private list<string> listprop; private map<string, string> mapprop; public list<string> getlistprop() { return listprop; } public void setlistprop(list<string> listprop) { this.listprop = listprop; } public map<string, string> getmapprop() { return mapprop; } public void setmapprop(map<string, string> mapprop) { this.mapprop = mapprop; } }
and below application.yaml file above property configuration class.
storage: listprop: - listvalue1 - listvalue2 mapprop: key1: mapvalue1 key2: mapvalue2
if using application.properties file instead of .yaml file
storage.listprop[0]=listvalue1 storage.listprop[1]=listvalue2 storage.mapprop.key1=mapvalue1 storage.mapprop.key2=mapvalue2
Comments
Post a Comment