java - 在 Web 应用程序中加载属性文件

标签 java web-applications

<分区>

到目前为止,我能够通过 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 ?

请指教

谢谢

最佳答案

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

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

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

关于java - 在 Web 应用程序中加载属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17238142/

相关文章:

tomcat - 从 Spring WebApp 运行 shell 命令

python - 如何通过python脚本获取 "Google Dictionary"的内容

java - 还有其他创建 servlet 实例的方法吗?

java - 具有自定义状态的线程

postgresql - 数据库连接在Golang中出错了吗?

php - 以图形方式创建和管理文档结构的应用程序

java - 无法将按钮文本设置为 "<<<"

java - 查询中的单个添加标量

java - 如何根据键值将 2 个 ArrayList<HashMap<String, String>> 合并为 1 个?

javascript - 在 PhoneGap 浏览器中存储用户数据