我有一個 Java maven 專案,其 Jar 檔案在其他專案中用作 SDK 庫。此 SDK 用于呼叫 prod 環境中默認存在的其他 API。我想通過覆寫一個變數讓它在測驗環境中運行。
public class Routes {
public Map<String, String> routes;
private static String _baseUrl = "https://production-env.com";
@SuppressWarnings("serial")
public Routes() {
routes = new HashMap<String, String>() {
{
put("api.login", "/rest/login");
put("api.register", "/rest/register");
}
};
}
}
這是 Routes 類,其中包含其他類使用的 prod/testing 的基本 URL。我想擁有與_baseUrl
上面相同的默認值。但我希望能夠"https://testing-env.com"
在運行 Jar 時覆寫它。而且我不想為此添加一個公共設定器。我希望能夠通過在運行 Jar 檔案時傳遞一些引數來做到這一點。
uj5u.com熱心網友回復:
正如@Thomas 在評論中提到的:
// Get system property with default if property does not exist
public class Routes {
private static String _baseUrl = System.getProperty("base.url", "https://production-env.com");
public Map<String, String> routes;
@SuppressWarnings("serial")
public Routes() {
routes = new HashMap<String, String>() {
{
put("api.login", "/rest/login");
put("api.register", "/rest/register");
}
};
}
}
執行jar時設定屬性
java -Dbase.url=https://dev-env.com -jar my.jar
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/507978.html