我尝试在 JBossFuse/karaf 上运行的 Java 中加载属性文件。
该文件位于 $[karaf.home]/etc/bean.properties
代码能够很好地加载捆绑包中的属性,但现在我尝试从项目本身中排除属性并且代码抛出 Nullpointer-Exception。
路径在我的开发机器上正确解析为
C:\Users\someone\devstudio\runtimes\jboss-fuse-6.3.0.redhat-135\etc\bean.properties
可以在蓝图 XML 中加载属性文件来配置 bean,但要访问 bean,我的代码需要 CamelContext。由于我有一些无需交换/上下文/注册表即可访问的静态代码块,因此我还希望能够在 Java 中加载属性。
这两个函数都会抛出 NullPointerException,我猜,这是因为代码在 Fuse 中运行。
public static Properties getProperties(String location) {
Properties prop = new Properties();
InputStream input = null;
try {
input = PropertyLoader.class.getClassLoader().getResourceAsStream(location);
prop.load(input);
} catch (IOException ex) {
log.error("Error loading properties file from: " + location, ex);
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
log.error(e);
}
}
}
return prop;
}
public static Properties getPropertiesFromFilesystem(String location) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(location);
prop.load(input);
} catch (IOException ex) {
log.error("Error loading properties file from: " + location, ex);
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
log.error(e);
}
}
}
return prop;
}
例外:
java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434)[:1.8.0_91] at java.util.Properties.load0(Properties.java:353)[:1.8.0_91] at java.util.Properties.load(Properties.java:341)[:1.8.0_91] at com.mycompany.util.PropertyLoader.getProperties(PropertyLoader.java:19)[319:camel-archetype-blueprint:0.0.14]在 com.mycompany.camel.blueprint.MyProcessor.process(MyProcessor.java:21)[319:camel-archetype-blueprint:0.0.14] 在 org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63 )[231:org.apache.camel.camel-core:2.17.0.redhat-630135] 在 org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)[231:org.apache.camel.骆驼核心:2.17.0.redhat-630135] 在 org.apache.camel。org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java: 196)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] 在 org.apache.camel.processor.Pipeline.process(Pipeline.java:121)[231:org.apache.camel .camel-core:2.17.0.redhat-630135] 在 org.apache.camel.processor.Pipeline.process(Pipeline.java:83)[231:org.apache.camel.camel-core:2.17.0.redhat -630135] 在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] 在 org.apache.camel。 component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:192)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] 在 org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:76)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] 在 java。 util.TimerThread.mainLoop(Timer.java:555)[:1.8.0_91] 在 java.util.TimerThread.run(Timer.java:505)[:1.8.0_91]
任何帮助将不胜感激。