在log4j附加程序中从WEB-INF文件夹加载属性文件 [英] Loading properties file from WEB-INF folder in a log4j appender
问题描述
我们正在为我们的应用程序编写自定义log4j附加程序。附加程序应将其事件记录到数据库中。现在,我遇到的问题是建立数据库连接。我们的jdbc设置位于一个名为jdbc.properties的文件中,该文件位于WEB-INF文件夹的正下方。
We're writing a custom log4j appender for our application. The appender should log its events to a database. Now the problem I'm having is setting up the database connection. Our jdbc settings are in a file called jdbc.properties which is located directly under the WEB-INF folder.
我尝试使用以下代码访问属性文件
I've tried accessing the properties file using the following code
InputStream stream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jdbc.properties");
...,但流导致为空。有什么想法可以从log4j附加程序的WEB-INF文件夹中加载属性文件,而无需将属性文件移动到另一个位置?
... but stream results in being null. Any ideas how I can load a properties file from the WEB-INF folder in a log4j appender without moving the properties file to another location?
推荐答案
也许可以尝试,
String path =Thread.currentThread().getContextClassLoader().getResource("/").toURI().resolve("../jdbc.properties").getPath();
Properties ps=new Properties();
ps.load(new FileInputStream(path));
这篇关于在log4j附加程序中从WEB-INF文件夹加载属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!