有我的解決問題的方法思路:
config.properties
通過設置到您的JVM的參數-DconfigurationFile
來定義配置文件。然後嘗試在您的classpath
(內部jar)中找到confiFile
(如果未找到),那麼將搜索文件系統。那麼,最後的屬性將被JVM參數覆蓋。
Properties prop = new Properties();
String configFile = System.getProperty("configurationFile",defaultConfigurationFile);
try {
InputStream classPathIo = getClass().getClassLoader().getResourceAsStream(configFile);
if(classPathIo != null) {
prop.load(classPathIo);
} else {
prop.load(new FileReader(configFile));
} catch (FileNotFoundException e) {
log.warn("The config file {} cannot be found. It can be setup by -DconfigurationFile parameter.",configFile);
} catch (IOException e) {
log.warn("The config file {} is not readable.",configFile);
} finally {
log.info("Configuration loaded! {} values found from configFile {}.",prop.entrySet().size(),configFile);
prop.putAll(System.getProperties());
}
log4j.properties
該溶液是用以下JVM參數的:萬一
-Dlog4j.configuration={path to file}
如果文件不在類路徑(在WEB-INF /類)但在你的磁盤上某處,使用文件:,如
-Dlog4j.configuration=file:/somewhere/on/disk/log4j.properties
hibernate.cfg.xml
我不知道該怎麼做。無論如何,很難在發佈之後配置持久性,因爲配置很難綁定到實現。我認爲可以將它保存在classpath中。
我很抱歉,我以前從未使用過JVM參數...我是否爲此代碼創建了一個新文件-Dlog4j.configuration = file:/ somewhere/on/disk/log4j.properties'或者我擁有哪個文件添加此代碼? –
你如何運行代碼? Stnadalone?像Tomcat這樣的容器? –
我只是做一個Java項目不是Web項目或其他...在未來,這個Java將運行在命令提示符(可執行jar)與此命令'java -jar'任何想法? –