Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring为多租户环境中的占位符配置application.properties_Java_Spring_Placeholder_Application.properties - Fatal编程技术网

Java Spring为多租户环境中的占位符配置application.properties

Java Spring为多租户环境中的占位符配置application.properties,java,spring,placeholder,application.properties,Java,Spring,Placeholder,Application.properties,我有一个多租户环境,所以我需要在运行时更改application.properties中的一些路径,以使用特定租户的文件夹。 例如,在我的应用程序属性中: image.avatars=C:/Users/Public/Pictures/Sample Pictures/${tenant}/Avatars/ 在我的课堂上我使用 @Autowired private Environment env; private static final String DIRECTORY_USER_IMAGE =

我有一个多租户环境,所以我需要在运行时更改application.properties中的一些路径,以使用特定租户的文件夹。 例如,在我的应用程序属性中:

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
,但我不明白如何在我的案例中使用它,因为它只有一个参数,如so
env.resolveRequiredPlaceholders(TenantContext.getCurrentTenant())

有没有一种简单的方法可以在不操纵字符串的情况下更改占位符(替换)?
我认为env.resolveRequiredPlaceholder需要属性名和占位符的varargs,但它是不同的。
谢谢

这可能不是你想要的(因为我很难理解你的场景),但是把

image.avatars=C:/Users/Public/Pictures/Sample Pictures/${tenant}/Avatars/
应用程序.properties中,使用

@Value("${image.avatars}")
private String DIRECTORY_USER_IMAGE;
在您的bean/服务中,使用命令行参数运行应用程序,如

--tenant="FooBar"
这将为
DIRECTORY\u USER\u IMAGE
提供值
C:/Users/Public/Pictures/Sample Pictures/FooBar/Avatars/
,您可以根据需要更改CLI参数。但是请注意,
目录\u用户\u图像
不再是
静态最终


我希望我没有弄错您的要求。

您可以使用
String.format()

只需在属性中使用
%s

image.avatars=C:/Users/Public/Pictures/Sample Pictures/%s/Avatars/
代码中的

String.format(imageavatars, tenant)