属性文件 [英] properties file in web app
问题描述
我试图将我的配置数据外包给我的webapp,所以我可以在部署后进行配置。 p>
这是我的属性服务:
public class PropertiesService {
属性属性;
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);
}
}
在调试后,我注意到变量流保持为null!但我不知道为什么 - 。
需要帮助: - )
这里的错误日志:
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
to config package in source,一旦构建,它将转到 /WEB-INF/classes/config/config.properties
和 this.getClass()。getResourceAsStream(/ config / config.properties);
strong>更新
,然后我在同一个项目中创建了一个 Service
类,它有一个方法。
public static InputStream getConfigAsInputStream(){
return Service.class.getResourceAsStream(/ config / config.properties);
}
比较您的
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.
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
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
and this.getClass().getResourceAsStream("/config/config.properties");
Update
and then I created a Service
Class in the same project which has a method.
public static InputStream getConfigAsInputStream(){
return Service.class.getResourceAsStream("/config/config.properties");
}
This works..!!
Compare yours
这篇关于属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!