我有一个多租户环境,所以我需要在运行时更改应用程序属性中的一些路径以使用特定租户的文件夹。 例如,在我的应用程序属性中:
image.avatars=C:/Users/Public/Pictures/Sample Pictures/${tenant}/Avatars/
在我的课堂上我使用
@Autowired
private Environment env;
private static final String DIRECTORY_USER_IMAGE = "image.avatars";
.....Method
env.getRequiredProperty(DIRECTORY_USER_IMAGE)
我读过关于env.resolveRequiredPlaceholders
但我不明白如何在我的情况下使用它,因为它只有一个参数,就像env.resolveRequiredPlaceholders(TenantContext.getCurrentTenant())
.
有没有一种简单的方法可以在不操作字符串(带替换)的情况下更改占位符?
我认为 env.resolveRequiredPlaceholder 需要属性的名称和占位符的变量,但它是不同的。 谢谢
您可以使用String.format()
.
只需在属性中使用%s
image.avatars=C:/Users/Public/Pictures/Sample Pictures/%s/Avatars/
而在代码中
String.format(imageavatars, tenant)
这可能不完全是你想要的(因为我很难理解你的场景),但是把
image.avatars=C:/Users/Public/Pictures/Sample Pictures/${tenant}/Avatars/
在您的application.properties
中,并使用
@Value("${image.avatars}")
private String DIRECTORY_USER_IMAGE;
在您的 Bean/服务中并使用命令行参数运行应用程序,例如
--tenant="FooBar"
这将DIRECTORY_USER_IMAGE
提供C:/Users/Public/Pictures/Sample Pictures/FooBar/Avatars/
值,您可以根据需要更改 CLI 参数。但请注意,DIRECTORY_USER_IMAGE
不再static final
了。
我希望我符合您的要求。