簡體   English   中英

Java Web 應用程序和屬性文件

[英]Java web app and properties file

我正在嘗試使用 Java Jersey 創建一個網絡應用程序。 該項目(動態 Web 項目)在運行時應該具有可由客戶端/服務器管理員更改的參數。 管理員無權訪問數據庫。 在將我們的應用程序轉換為 Web 服務之前,我們在主 jar 旁邊的文件夾中定義了屬性文件。 有沒有辦法在動態網絡應用程序中擁有類似的東西? 我的想法是擁有一個包含屬性文件的文件夾,服務器管理員可以訪問和更改(以便 Web 服務可以在運行時更改它的工作參數)。 將屬性存儲在數據庫而不是文件中是不行的。 它必須是一個配置文件 :) 你知道我應該如何將我的屬性文件放在相對路徑中,以便服務器管理員使用該文件可以在 WS 已經部署和工作時更改某些 Web 服務參數嗎?

根據您的要求,您可以使用屬性文件,但問題是 WS 僅在啟動時加載屬性文件。 因此,您必須在程序代碼中加載該屬性文件。 並且您需要在一段時間后重新加載文件。 請參閱以下代碼供您參考:

公共類資源處理器{

/** The service end points. Creates properties file object. */
Properties serviceEndPoints = new Properties();


/** The resource handler. */
private static ResourceHandler resourceHandler = new ResourceHandler();

/**
 * Instantiates a new resource handler.
 */
ResourceHandler() {
    try {
           /* Load the properties file from class path, you can use reguler file path instead of this. */
        serviceEndPoints.load(this.getClass().getResourceAsStream(
                "serviceEndPoints.properties"));

    } catch (IOException e) {
    }
}

/**
 * Instance.
 * 
 * @return the resource handler
 */
public static synchronized ResourceHandler instance() {
    if (resourceHandler == null)
        resourceHandler = new ResourceHandler();
    return resourceHandler;
}

/**
 * Gets the service end points.
 * 
 * @param key
 *            the key
 * @return the service end points
 * Use this method to get values of parameters from properties file.
 */
public String getServiceEndPoints(String key) {
    return ((String) serviceEndPoints.get(key));
}

}

上面的代碼用於一次性加載文件。 由於您想獲取那些立即更改的參數,因此您必須在一段時間后加載該文件。 但肯定有可能!

暫無
暫無

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

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