0

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

luca
  • 3,248
  • 10
  • 66
  • 145

2 Answers2

1

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)
dehasi
  • 2,644
  • 1
  • 19
  • 31
0

This might be not exactly what you want (because I struggle to understand your scenario), but what about putting

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

in your application.properties, and using

@Value("${image.avatars}")
private String DIRECTORY_USER_IMAGE;

in your bean/service and running the app with a command line argument like

--tenant="FooBar"

This would give DIRECTORY_USER_IMAGE the value C:/Users/Public/Pictures/Sample Pictures/FooBar/Avatars/ and you could change the CLI argument according to your needs. But be aware that DIRECTORY_USER_IMAGE is not static final anymore.

I hope I got your requirements right.

bkis
  • 2,530
  • 1
  • 17
  • 31