Sve*_*ven 3 java configuration properties
首先,有很多解决方案,我已经阅读了很多解决方案.但由于某种原因,我没有让它发挥作用.
我正在尝试将我的配置数据外包给我的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");
}
}
Run Code Online (Sandbox Code Playgroud)
调试后我注意到变量流仍为空!但我不知道为什么 - .-
需要帮忙 :-)
这里的错误日志:
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)
Run Code Online (Sandbox Code Playgroud)
更新
我现在做以下事情:
properties.load(this.getClass().getResourceStream("/config/config.properties"));
Run Code Online (Sandbox Code Playgroud)
我仍然得到一个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");
}
Run Code Online (Sandbox Code Playgroud)
这工作.. !!
比较你的
| 归档时间: |
|
| 查看次数: |
9602 次 |
| 最近记录: |