【问题标题】:Load properties file within a web app [duplicate]在 Web 应用程序中加载属性文件 [重复]
【发布时间】:2013-06-18 18:08:52
【问题描述】:

到目前为止,我可以通过 ClassLoader 加载属性文件,因为它是一个独立的应用程序 -> jar

/**
 * Path and filename for {@code.properties}.
 */
private String propertiesFileName = "./com/config/resources/config.properties";

propertiesFromFile.load(ClassLoader.getResourceAsStream(propertiesFileName));

现在我需要在 Web 应用程序中使用这个 jar,问题是通过 ClassLoader 我得到 NULL

怎样才能让jar里面的那个能读取属性文件呢?

我为什么要通过 maven ?

请指教

谢谢

【问题讨论】:

  • 你读过this question吗?
  • 是的,我正在检查这个答案,基本上我需要删除点“/com/config/resources/config.properties”。这将在将 jar 添加到 Web 应用程序库时有效,但在运行 jar 时无效。
  • 我自己编写了一些代码来执行此操作,直到我意识到让 spring 读取我的属性文件更容易。您可以将文件位置作为 jvm 参数传递并覆盖一组基本属性。最好的属性是可注入的。

标签: java web-applications


【解决方案1】:

当我使用java.util.Properties 时,我在不同平台上遇到了不同行为的类似问题。最后我决定使用 Apache Commons - Java Configuration API:-

PropertiesConfiguration properties = new PropertiesConfiguration("my.properties");
String propertyValue = properties.getString("prop.key");

我的属性文件的路径是在我启动容器时在我的 JVM 类路径前缀中设置的。

【讨论】:

  • 这也是一个选项,谢谢
猜你喜欢
  • 1970-01-01
  • 2015-08-28
  • 2018-06-30
  • 2016-01-31
  • 1970-01-01
  • 2015-06-15
  • 2011-03-10
  • 2012-09-12
  • 1970-01-01
相关资源
最近更新 更多