簡體   English   中英

無法找到bundle java.util的資源

[英]Can't find resource for bundle java.util

我有兩個屬性文件:

config-app.properties

config-dev.properties

我試着從第二個女巫那里讀到第二個那樣的女巫:

java代碼:

String smtpHostName = ResourceBundle.getBundle("config-app").getString("smtp.host.name");

config-app.properties

smtp.host.name  =${smtp.host.name.env}

config-dev.properties

smtp.host.name.env  =smtp.gmail.com

但我有這樣的信息:

Can't find resource for bundle java.util.PropertyResourceBundle

編輯:代碼格式。

假設config-app.properties存在於類路徑中(在根目錄中),這應該可行。 您不應該收到錯誤“無法找到bundle java.util.PropertyResourceBundle的資源”。

但是,通過替換$ {smtp.host.name.env},您嘗試執行的操作將無法與ResourceBundle一起使用。 你需要另一個庫來做更復雜的事情。

更新

目前尚不清楚你要做什么,但假設你想要開發的配置文件和另一個用於生產的配置文件,你可以嘗試做這樣的事情:

Properties defaultProperties = new Properties();
defaultProperties.load(new FileInputStream("config-app.properties"));

Properties properties = new Properties(defaultProperties);
if (isDev()) {
  properties.load(new FileInputStream("config-dev.properties"));
} else {
  properties.load(new FileInputStream("whatever.properties"));
}

properties.getProperty("smtp.host.name");

這將在config-app.properties具有默認設置,可以根據需要在config-dev.propertieswhatever.properties覆蓋。 在這種情況下,保持必須相同。

config-app.properties:

smtp.host.name =localhost

config-dev.properties:

smtp.host.name =smtp.gmail.com

最后一點,前面的代碼是從文件系統加載文件,如果你在類路徑中有它們,請使用類似下面的代碼:

defaultProperties.load(Classname.class.getClassLoader().getResourceAsStream("config-app.properties"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM