0

I have a properties file that looks like this

server.se.host
server.se.port

server.us.host
server.us.port

and so on. It's a list of host/port used for different countries (Sweden and US in this example). These can also be set as environment variables in the form SERVER_SE_HOST and so on.

The code however, doesn't know what countries exists at build time so I need to dynamically find these variables, including the ones only available as environment variables at run time.

Ideally I'd like to get configration class with a dynamic prefix, something like

Config countrySpecificConfig = ConfigMagic.getFromPropertiesWithPrefix("server." + countryCode);

If that is not possible I would be happy with

String countrySpecificHost = PropertiesMagic.get("server." + countryCode + ".host"); 
String countrySpecificPort = PropertiesMagic.get("server." + countryCode + ".port"); 

but I haven't found a way to do that either.

Thanks.

Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
  • Reading system environment variables can be done quite easily using `System#getenv`. As for what you want to achieve I suppose this is what you're looking for: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/support/PropertiesLoaderUtils.html – akortex Jan 19 '20 at 21:09
  • Yes, reading properties files is easy, so is reading environment variables. I'm trying to find a way to do "the spring magic" where variables are read from multiple sources and places with different priority. – Andreas Wederbrand Jan 19 '20 at 21:20

0 Answers0