简体   繁体   中英

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.

And in a class I need to read these variables.

I am using the config library, 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.

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.

I am wondering how to get this "classpath resource basename" of the config.conf file?

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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