在java应用程序中读取外部属性文件

时间:2013-03-31 08:17:00

标签: java tomcat properties

我想从Web应用程序外部加载属性文件。获取属性文件的代码如下所示。

 Properties p = new Properties();
 p.load(this.getClass().getClassLoader().getResourceAsStream("a.properties"));

我正在使用tomcat服务器,我想将属性文件放在tomcat服务器中。我可以将它放在服务器中以便在运行应用程序时在类路径中使用它吗?我不想更改上面的代码,因为我必须运行相同的应用程序无关的服务器

2 个答案:

答案 0 :(得分:3)

我推荐第一个选项。将 a.properties 放在类路径中。然后加载:

Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("a.properties"));

这样,您可以加载相对于类路径的“根”的属性。建议使用Thread.currentThread()。getContextClassLoader()为此返回的ClassLoader。

答案 1 :(得分:2)

基本上有三种方式:你可以看到剩下的部分 Where to place and how to read configuration resource files in servlet based application?

相关问题