Search code examples
javaspringplaceholderapplication.properties

Spring configure application.properties for placeholder in multitenant environment


I have a multi tenant environment so I need to change some path from application.properties in runtime to use the folder of specific tenant. For example in my application properties:

image.avatars=C:/Users/Public/Pictures/Sample Pictures/${tenant}/Avatars/

in my class I use

@Autowired
private Environment env;
private static final String DIRECTORY_USER_IMAGE = "image.avatars";
.....Method
    env.getRequiredProperty(DIRECTORY_USER_IMAGE)

I read about env.resolveRequiredPlaceholders but I don't understand how it can be used in my case since it has only one parameter like so env.resolveRequiredPlaceholders(TenantContext.getCurrentTenant()).
Is there a simple way to change the placeholder without manipulate String(with replace)?
I thought that env.resolveRequiredPlaceholders required the name of properties and the varargs of placeholder but it is different. Thanks


Solution

  • You can use String.format().

    Just use %s in properties

    image.avatars=C:/Users/Public/Pictures/Sample Pictures/%s/Avatars/
    

    And the in the code

    String.format(imageavatars, tenant)