[英]properties file in web app
首先,有很多解决方案,我已经阅读了很多解决方案。 但由于某种原因,我没有让它发挥作用。
我正在尝试将我的配置数据外包给我的webapp,以便我可以在部署之后进行配置。
那是我的物业服务:
public class PropertiesService {
Properties properties;
public PropertiesService() {
try {
properties = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream stream = classLoader.getResourceAsStream("META-INF/config.properties");
properties.load(stream);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getHost(){
return properties.getProperty("server_host");
}
public String getServerName(){
return properties.getProperty("server_naming");
}
}
调试后我注意到变量流仍为空! 但我不知道为什么 - .-
需要帮忙 :-)
这里的错误日志:
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
更新
我现在做以下事情:
properties.load(this.getClass().getResourceStream("/config/config.properties"));
我仍然得到一个nullPointerException
取出META-INF
,将它放在src/config
直接配置源代码中的包,在构建时它将转到/WEB-INF/classes/config/config.properties
和this.getClass().getResourceAsStream("/config/config.properties");
更新
然后我在同一项目中创建了一个具有方法的Service
类。
public static InputStream getConfigAsInputStream(){
return Service.class.getResourceAsStream("/config/config.properties");
}
这工作.. !!
比较你的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.