我正在使用Hibernate 5.0.3和tomcat8服務器。如何更改Java bean中的「常量」值(hibernate)?
在返回JSON響應與休息的第一部分(對象)我有關於錯誤消息,httpcode,api版本和應用程序版本的數據。它被稱爲MessageJson,第二部分是用戶請求的對象/返回值。
如何在不重啓服務器並上傳新的.war的情況下實時或更改appVersion屬性的值?屬性約被標記爲下面的代碼:
public class MessageJson {
private int httpCode;
private String errorMessage;
private String apiVer;
private String appVersion ;
public MessageJson (int httpCode, String errorMessage) {
this.httpCode = httpCode;
this.errorMessage = errorMessage;
this.apiVer = "0.905";
this.appVersion = "1.350";//How to change this value without restarting server? And without reading from DB on every single request from api?
}
}
這裏是從API返回完整的對象/的JSON結構:
public class ResponseJson {
private MessageJson message;
private Object body;
public ResponseJson (MessageJson message, Object body) {
this.body = body;
this.message = message;
}
是否冬眠讀取值從的.properties文件每次或有沒有保存的值在RAM中?
什麼是最有效的方法來更新這種變量值,而不需要在每次調用時從數據庫請求它? 有沒有辦法像上面的例子一樣將它保存在內存中,只需要檢查它在數據庫或特定文件中更改的時間?在請求檢查時,新值將覆蓋班級中的現有值。
示例:在每個請求值處返回表單類(1.350)。管理員請求調用它後,DB有新的值「1.500」,舊的(1.350)被覆蓋。來自用戶的每個下一個呼叫只會返回1.500而不在數據庫或文件中進行檢查。
感謝您的建議。我已經嘗試過'commons-configuration2'(新的),但它只是從文件讀取,但我無法讓它自動重新加載值。我也嘗試過'v1.10'。它也剛剛讀取服務器重新啓動後的值,並且FileChangedReloadingStrategy什麼也不做。我在'/ resources /'文件夾中有我的'config.properties'文件。當我用手改變屬性的值時,它不會讀取它。 – AndroidTank
@AndroidTank你好,你有機會看看這個[鏈接](https://commons.apache.org/proper/commons-configuration/userguide/howto_reloading.html) 你可以發佈你有什麼試過嗎? – Kamal