【问题标题】:Loading Property File from Filesystem in Jboss Fuse / Karaf throws Nullpointer在 Jboss Fuse / Karaf 中从文件系统加载属性文件抛出 Nullpointer
【发布时间】:2017-10-10 21:57:42
【问题描述】:

我尝试在 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 在 java.util.Properties$LineReader.readLine(Properties.java:434)[:1.8.0_91] 在 java.util.Properties.load0(Properties.java:353)[:1.8.0_91] 在 java.util.Properties.load(Properties.java:341)[:1.8.0_91] 在 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.camel-core:2.17.0.redhat-630135] 在 org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468)[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.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]

任何帮助将不胜感激。

【问题讨论】:

  • 鉴于您的 cmets,我强烈建议您阅读“OSGi in Action”一书

标签: java jboss jbossfuse karaf property-files


【解决方案1】:

如果没有更完整的示例,您不确定为什么要获得 NPE。如果你需要使用没有路由的属性,你应该使用 Camel 的属性占位符工具:

https://access.redhat.com/documentation/en-us/red_hat_jboss_fuse/6.3/html/apache_camel_development_guide/basicprinciples#BasicPrinciples-PropPlaceholders

【讨论】:

    【解决方案2】:

    不要那样做。你在找麻烦。

    1. 以 OSGi 方式加载属性(使用 .cfg 作为扩展名和蓝图 property-placeholder bean)
      如果文件更改(如果您愿意),您还可以获得通知
    2. 即使您只使用静态方法,也可以将它们注入 bean。
      除非您非常清楚自己在做什么,否则不要将托管 bean 与非托管静态代码混用。

    如果某些“static”代码需要属性意味着它是有状态的,并且这个类应该被实例化为一个bean。

    【讨论】:

    • 意味着我应该获取当前的“property-bean”并将其传递给初始化?例如,我需要属性来定义数据库连接
    • 是的,在另一个 bean 中设置“property-bean”。如果您需要访问数据库,为什么不使用数据源?
    猜你喜欢
    • 1970-01-01
    • 2015-04-02
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 2017-10-04
    相关资源
    最近更新 更多