4

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

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

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

4

2 回答 2

3

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

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

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

于 2013-03-31T08:26:54.700 回答
2

基本上有三种方式:剩下的部分你可以在 Where to place 以及如何在基于 servlet 的应用程序中读取配置资源文件中看到?

于 2013-03-31T08:23:13.280 回答