【问题标题】:How to get the classpath resource basename of a config file in Spring proejct?如何在 Spring 项目中获取配置文件的类路径资源基名?
【发布时间】:2020-07-14 19:54:47
【问题描述】:

在我的 Spring 项目中,我创建了一个 config.conf 来存储一些变量。

在课堂上我需要读取这些变量。

我正在使用confighttps://github.com/lightbend/config

正如文档所说,我尝试使用

private static final Config config = ConfigFactory.load(basename here goes here);

加载config.conf 文件。

通过文档https://lightbend.github.io/config/latest/api/com/typesafe/config/ConfigFactory.html#load-java.lang.String-,此方法接收一个字符串作为类路径资源基名。

我想知道如何获取config.conf 文件的这个“类路径资源基名”?

我的项目结构:

谢谢!

【问题讨论】:

    标签: java spring spring-boot config


    【解决方案1】:

    你可以像这样访问文件。

    URL url = EmailService.class.getClassLoader().getResource("config.conf");
    

    要加载配置,您可以访问如下路径

    config.load(url.getPath());
    

    【讨论】:

      猜你喜欢
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 2023-01-27
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多