[英]How to get the classpath resource basename of a config file in Spring proejct?
In my Spring proejct, I created a config.conf
to store some variables.在我的 Spring 项目中,我创建了一个config.conf
来存储一些变量。
And in a class I need to read these variables.在 class 中,我需要读取这些变量。
I am using the config
library, https://github.com/lightbend/config .我正在使用config
库https://github.com/lightbend/config 。
And as the document says, I tried to used正如文件所说,我试图使用
private static final Config config = ConfigFactory.load(basename here goes here);
to load that config.conf
file.加载该config.conf
文件。
And by the document, https://lightbend.github.io/config/latest/api/com/typesafe/config/ConfigFactory.html#load-java.lang.String- , this method receives a string as classpath resource basename.根据文档https://lightbend.github.io/config/latest/api/com/typesafe/config/ConfigFactory.html#load-java.lang.String- ,该方法接收一个字符串作为类路径资源基名。
I am wondering how to get this "classpath resource basename" of the config.conf
file?我想知道如何获取config.conf
文件的“类路径资源基名”?
My project's structure:我的项目结构:
Thanks!谢谢!
you can access file like this.你可以像这样访问文件。
URL url = EmailService.class.getClassLoader().getResource("config.conf");
to load config, you can access path like below要加载配置,您可以访问如下路径
config.load(url.getPath());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.