【问题标题】:Spring configure application.properties for placeholder in multitenant environmentSpring在多租户环境中为占位符配置application.properties
【发布时间】:2019-08-08 17:12:02
【问题描述】:

我有一个多租户环境,所以我需要在运行时更改 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 的信息,但我不明白如何在我的情况下使用它,因为它只有一个参数,例如env.resolveRequiredPlaceholders(TenantContext.getCurrentTenant())
有没有一种简单的方法来更改占位符而无需操作字符串(带替换)?
我认为 env.resolveRequiredPlaceholders 需要属性的名称和占位符的可变参数,但它是不同的。 谢谢

【问题讨论】:

    标签: java spring placeholder application.properties


    【解决方案1】:

    您可以使用String.format()

    只需在属性中使用%s

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

    代码中的

    String.format(imageavatars, tenant)
    

    【讨论】:

      【解决方案2】:

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

      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

      我希望我能满足您的要求。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-06
        • 1970-01-01
        • 2016-07-24
        • 2021-12-11
        • 2023-03-07
        • 1970-01-01
        相关资源
        最近更新 更多