java - Using ConfigurationProperties to fill Map in generic way -


i'm wondering, if there generic way fill map properties know prefix.

assuming there bunch of properties like

namespace.prop1=value1 namespace.prop2=value2 namespace.idontknowthisnameatcompiletime=anothervalue 

i'd have generic way fill property inside map, like

@component @configurationproperties("namespace") public class mygenericprops {     private map<string, string> propmap = new hashmap<string, string>();      // setter , getter propmap omitted      public set<string> returnallkeys() {         return propmap.keyset();     } } 

or there convenient way collect properties prefix, instead of iterating on propertysources in environment?

thanks hansjoerg

as long you're happy having every property added map, rather don't know in advance, can @configurationproperties. if want grab that's beneath namespace need use empty prefix , provide getter map named namespace:

@configurationproperties("") public class customproperties {      private final map<string, string> namespace = new hashmap<>();      public map<string, string> getnamespace() {         return namespace;     }  } 

spring boot uses getnamespace method retrieve map can add properties it. these properties:

namespace.a=alpha namespace.b=bravo namespace.c=charlie 

the namespace map contain 3 entries:

{a=alpha, b=bravo, c=charlie} 

if properties nested more deeply, example:

namespace.foo.bar.a=alpha namespace.foo.bar.b=bravo namespace.foo.bar.c=charlie 

then you'd use namespace.foo prefix , rename namespace , getnamespace on customproperties bar , getbar respectively.

note should apply @enableconfigurationproperties configuration enable support @configurationproperties. can reference beans want processed using annotation, rather providing @bean method them, or using @component have them discovered component scanning:

@springbootapplication @enableconfigurationproperties(customproperties.class) public class yourapplication {     // … } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -