[英]properties file in web app
First, there are a lot of solutions out there and I have already read a lot of them. 首先,有很多解决方案,我已经阅读了很多解决方案。 But for some reason I don't get it working.
但由于某种原因,我没有让它发挥作用。
I am trying to outsource my config data for my webapp, so that I can cnange it after deployment. 我正在尝试将我的配置数据外包给我的webapp,以便我可以在部署之后进行配置。
That is my properties service: 那是我的物业服务:
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");
}
}
After debugging I noticed that the variable stream remains null! 调试后我注意到变量流仍为空! But I don't know why -.-
但我不知道为什么 - .-
Need help :-) 需要帮忙 :-)
here the error log: 这里的错误日志:
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)
Update 更新
I do the following now: 我现在做以下事情:
properties.load(this.getClass().getResourceStream("/config/config.properties"));
And I still get a nullPointerException 我仍然得到一个nullPointerException
Take out from META-INF
, put it in src/config
direct to config package in source , upon build it will go to /WEB-INF/classes/config/config.properties
取出
META-INF
,将它放在src/config
直接配置源代码中的包,在构建时它将转到/WEB-INF/classes/config/config.properties
and this.getClass().getResourceAsStream("/config/config.properties");
和
this.getClass().getResourceAsStream("/config/config.properties");
Update 更新
and then I created a Service
Class in the same project which has a method. 然后我在同一项目中创建了一个具有方法的
Service
类。
public static InputStream getConfigAsInputStream(){
return Service.class.getResourceAsStream("/config/config.properties");
}
This works..!! 这工作.. !!
Compare yours 比较你的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.