Search code examples
javaspring-bootapplication.properties

Dynamic key names in application.properties


I have application which acts as multiple delivery service. All clients must be configured trough application.properties, but I need to have separate all configurations with some id.

Example:

app.delivery.method.{clientId}=SFTP
app.{clientId}.sftp.username=some user
app.{clientId}.sftp.password=some pass
etc...

Is it possible to achieve something like this, with overriding some spring boot's parser for properties, or using some external library.

I have read almost all of documentation, and saw many examples, but didn't find anything.

Thanks in advance


Solution

  • This is how I solved a similar problem

    @Configuration
    @ConfigurationProperties(prefix = "app")
    public class PropertiesConfig {
        @Autowired
        private Environment env;
        ...
    
        public String getDeliveryMethod(String clientId) {
            return env.getProperty("app.delivery.method."+clientId);
        }
    
        public String getClientSftpUsername(String clientId) {
            return env.getProperty("app."+clientId+".sftp.username");
        }
        ...
    }