日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

死机简单配置

發布時間:2023/12/3 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 死机简单配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

編寫整個框架的目的是為了處理應用程序的配置。 我更喜歡一種簡單的方法。

如果通過配置我們的意思是“ 部署之間可能有所不同的所有內容 ”,那么我們應該嘗試使配置保持簡單。 在Java中,最簡單的選項是不起眼的屬性文件。 屬性文件的缺點是,當您希望應用程序接收更改時必須重新啟動它。 還是你

這是我在多個項目中使用的一種簡單方法:


public class MyAppConfig extends AppConfiguration {private static MyAppConfig instance = new MyAppConfig();public static MyAppConfig instance() {return instance;}private MyAppConfig() {this("myapp.properties");}public String getServiceUrl() {return getRequiredProperty("service.url");}public boolean getShouldStartSlow() {return getFlag("start-slow", false);}public int getHttpPort(int defaultPort) {return getIntProperty("myapp.http.port", defaultPort);}}

AppConfiguration類如下所示:

public abstract class AppConfiguration {private static Logger log = LoggerFactory.getLogger(AppConfiguration.class);private long nextCheckTime = 0;private long lastLoadTime = 0;private Properties properties = new Properties();private final File configFile;protected AppConfiguration(String filename) {this.configFile = new File(filename);}public String getProperty(String propertyName, String defaultValue) {String result = getProperty(propertyName);if (result == null) {log.trace("Missing property {} in {}", propertyName, properties.keySet());return defaultValue;}return result;}public String getRequiredProperty(String propertyName) {String result = getProperty(propertyName);if (result == null) {throw new RuntimeException("Missing property " + propertyName);}return result;}private String getProperty(String propertyName) {if (System.getProperty(propertyName) != null) {log.trace("Reading {} from system properties", propertyName);return System.getProperty(propertyName);}if (System.getenv(propertyName.replace('.', '_')) != null) {log.trace("Reading {} from environment", propertyName);return System.getenv(propertyName.replace('.', '_'));}ensureConfigurationIsFresh();return properties.getProperty(propertyName);}private synchronized void ensureConfigurationIsFresh() {if (System.currentTimeMillis() < nextCheckTime) return;nextCheckTime = System.currentTimeMillis() + 10000;log.trace("Rechecking {}", configFile);if (!configFile.exists()) {log.error("Missing configuration file {}", configFile);}if (lastLoadTime >= configFile.lastModified()) return;lastLoadTime = configFile.lastModified();log.debug("Reloading {}", configFile);try (FileInputStream inputStream = new FileInputStream(configFile)) {properties.clear();properties.load(inputStream);} catch (IOException e) {throw new RuntimeException("Failed to load " + configFile, e);}} }

這樣可以高效地讀取配置文件,并根據需要更新設置。 它支持默認的環境變量和系統屬性。 而且它甚至可以很好地記錄正在發生的事情。

  • 有關完整的源代碼和自動更新的不可思議的數據源,請參見以下要點:https://gist.github.com/jhannes/b8b143e0e5b287d73038

請享用!

翻譯自: https://www.javacodegeeks.com/2014/10/dead-simple-configuration.html

總結

以上是生活随笔為你收集整理的死机简单配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。