簡體   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