繁体   English   中英

如何在 Spring proejct 中获取配置文件的类路径资源基名?

[英]How to get the classpath resource basename of a config file in Spring proejct?

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

在 class 中,我需要读取这些变量。

我正在使用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文件的“类路径资源基名”?

我的项目结构:

在此处输入图像描述

谢谢!

你可以像这样访问文件。

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

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

config.load(url.getPath());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM