2017-05-11 53 views
0

我嘗試在JBossFuse/karaf上加載運行在Java中的屬性文件。從Jboss Fuse/Karaf文件系統中加載屬性文件Nullpointer

該文件位於$ [karaf.home] /etc/bean.properties

的代碼能夠加載包裏面罰款性質,但現在我試圖排除來自項目本身的屬性和該代碼會拋出一個空指針異常。

的路徑是正確的決定,我的開發機器上

C:\用戶\人\ DEVSTUDIO \運行時\ jboss的熔絲,6.3.0.redhat-135 \等\ bean.properties

可以在blueprint-XML中加載屬性File以配置Bean,但要訪問Bean,我的代碼需要CamelContext。由於我有一些沒有交換/上下文/註冊表的靜態代碼塊,我還希望能夠加載Java中的屬性。

兩種功能拋出NullPointerException異常,我想,那是因爲在代碼運行保險絲。

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] at com.mycompany.camel.blueprint.MyProcessor.process(MyProcessor.java:21)[319:camel-原型-藍圖:0.0.14] 在org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)[231:org.apache.camel.camel核:2.17.0.redhat-630135] 在org.apache.camel.processor的org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] 。 RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468)[231:org.apache.camel.camel核: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] at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)[231:org.apache.camel .camel-core:2.17.0.redhat-630135] at org.apache.camel.processor.Pipeline.process(Pipeline的.java:83)[231:org.apache.camel.camel核:2.17.0.redhat-630135]在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:有機.apache.camel.camel核: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] at org.apache.camel.component.timer.TimerConsumer $ 1.run(TimerConsumer.java:76)[231:org.apache.camel.camel-core:2.17.0。 redis-630135] at java.util.TimerThread.mainLoop(Timer.java:555)[:1.8.0_91] at java.util.TimerThread.run(Timer.java:505)[:1.8.0_91]

任何幫助將不勝感激。

+0

鑑於您的意見,我強烈建議您閱讀「OSGi in Action」一書 –

回答

1

不要做到這一點。你正在尋找麻煩。

  1. 負載特性OSGi的方式(使用.CFG作爲延伸和藍圖property-placeholder豆)
    你的,如果該文件修改時收到通知(如果你願意的話)
  2. 注入他們在一個bean的額外好處即使你只使用靜態方法。
    不要將託管bean與非託管靜態代碼混合,除非您非常清楚自己在做什麼。

如果有些「靜態」代碼要求性能意味着它是狀態,這個類值得實例化一個bean。

+0

意思是我應該得到當前的「property-bean」並將它傳遞給初始化?例如,我需要屬性來定義數據庫連接 – user2122552

+0

是的,在另一個bean中設置「property-bean」。如果您需要訪問數據庫,爲什麼不使用數據源? –

+0

@ user2122552請參閱http://stackoverflow.com/questions/43465689/configuring-database-connection-in-jboss-fuse/43476015#43476015 –