我试图在Eclipse的JSF Web应用程序项目中使用所有服务器和数据库连接读写属性文件。我正在使用log4j写入控制台。我的config.properties文件是:
dbserver=localhost
dbname=mydatabase;instance=myinstance
dbuser=myuser
dbpassword=mypassword
我将config.properties文件放置在webapp / WEB-INF / classes文件夹中(这是对的类路径吗?)。我已验证它可以在此特定位置正确读取文件,因为如果删除文件,它会损坏。
在我的托管bean中,我具有读取和写入config.properties文件的功能。
public void getSettings() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("config.properties");
Properties properties = new Properties();
try {
properties.load(input);
this.server = properties.getProperty("dbserver");
this.db = properties.getProperty("dbname");
this.user = properties.getProperty("dbuser");
this.pass = properties.getProperty("dbpassword");
logger.info("Config file successfully loaded!");
} catch (IOException e) {
logger.error("Loading Database Settings Error with " + e);
} finally {
if (input != null) {
try {
input.close();
logger.info("Closing config file...");
} catch (IOException e) {
logger.error("Error closing config file with " + e);
}
}
}
}
public void saveSettings() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Properties props = new Properties();
OutputStream out = null;
try {
props.setProperty("dbserver", this.server);
props.setProperty("dbname", this.db);
props.setProperty("dbuser", this.user);
props.setProperty("dbpassword", this.pass);
URL url = classLoader.getResource("config.properties");
File file = null;
try {
file = new File(url.toURI().getPath());
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// File f = new File("config.properties");
out = new FileOutputStream(file);
props.store(out, "This is an optional header comment string");
logger.info("Config file successfully saved!");
} catch (IOException io) {
logger.error("Saving configuration properties failed error with : " + io.getMessage());
} finally {
if (out != null) {
try {
logger.info("Closing config file...");
out.close();
} catch (IOException e) {
logger.error("Failed closing configuration properties file error with : " + e);
}
}
}
}
从属性文件读取数据时我从来没有遇到过问题,但是很难写入文件。该问题似乎已经通过指定解决了
URL url = classLoader.getResource("config.properties");
现在,如果将服务器名称从“ localhost”更改为“ 192.168.1.1”,即使刷新页面或重新启动服务器,我仍然可以看到新信息仍然存在。但是,当我打开config.properties文件时,我仍然看到
dbserver=localhost
当我期望看到
dbserver=192.168.1.1
即使文件仍然保持不变,该信息似乎仍会保留在其他位置?如何以及在何处访问属性文件的内容,以查看对其所做的更改?
修改WAR文件不是一个好主意(例如,某些Web服务器可能会注意到文件修改会重新部署您的应用程序,当服务器激起部署战时可能会出现问题等)。
我建议采用替代方法-一些可能性是:
将属性放置在数据库表中,以便可以轻松修改它们
使用外部属性文件覆盖“ Property.prop”文件设置。例如,您可以按如下所示进行操作。假定默认属性值捆绑在您的WAR中,并且修改后的值保存到其他路径,该路径是使用系统属性定义的-假设它称为CONFIG_LOCATION。现在,从捆绑软件中加载属性后,您还可以从此外部“ overrides.prop”文件中读取属性-这会覆盖“ Property.prop”中的默认设置:
PropertiesConfiguration pc1=new PropertiesConfiguration(a);
try(
FileReader propReader = new FileReader(System.getenv().get("CONFIG_FILES") +"/overrides.prop"){ pc1.load(propReader);
}
当您需要保存更改时,可以对“ overrides.prop”执行此操作-这将保存所有属性,不仅保存更改的属性,而且应该没有负面影响。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句