【问题标题】:How to load config file without any file extension in Spring boot如何在 Spring Boot 中加载没有任何文件扩展名的配置文件
【发布时间】:2019-11-04 19:28:16
【问题描述】:

我正在尝试找出在没有任何文件扩展名的 SpringBoot 应用程序中加载配置文件的选项。

例如我有 2 个配置文件 application.properties 和我提到的使用外部位置的秘密。 Spring 忽略 secrets 配置文件;但是,当我将 secrets 文件扩展名更改为 properties/yml 时,它可以工作。

-Dspring.profiles.active=local \
-Dspring.config.location=file:///<config location>/git/graphql-account/secrets,file:///<config location>/git/graphql-account/application-local.properties

我已经探索了Spring guide,但找不到解决方案。 任何建议如何解决这个问题?

注意:我正在使用 openshift 云,我需要加载它的秘密,这些秘密在 /opt/epaas/vault/secrets/secrets 中可用。我不允许更改文件扩展名。

【问题讨论】:

  • 为什么不将其命名为secrets.propertiessecrets.yml
  • 我正在使用 openshift 云,我需要从位置 - /opt/epaas/vault/secrets/secrets 加载它的秘密。我不允许更改文件扩展名

标签: java spring spring-boot


【解决方案1】:

一种可能的解决方案是使用@PropertySource 注释:

@SpringBootApplication
@PropertySource("file:/etc/secrets")
public class Demo {

   public static void main(...) {...}
}

/etc/secrets 是常规属性文件(键/值对,但没有扩展名)。

当然,如果要配置/etc/secrets的地方,可以在@PropertySource注解属性定义中使用SPEL:

@PropertySource(value="file:/${secrets.file.localtion}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    相关资源
    最近更新 更多