2017-08-13 43 views
0

我需要您的建議來獲取存儲在澤西Web服務的Java屬性文件中的配置數據的最佳方法,以便在由這些Web服務調用的幾個DAO類中使用它們。從Web服務中檢索java屬性文件中的配置數據

,我實現的解決方案是如下:

  • 創建一個Java屬性文件,並把它,我需要
  • 所有屬性的所有Java特性在應用程序監聽器文件,並把性能在ServletContext中屬性
  • 在Web服務中,我通過使用@Context註釋在servletContext上注入一個實例來檢索屬性,並將它們傳遞給任何需要它們的DAO函數的方法。

那麼這是一個很好的方法嗎?如果不是,你能否提出另一個解決方案?

回答

0

而不是將單個屬性作爲屬性來上下文將屬性對象放在上下文中並從該prop對象獲取所需的屬性。

0

爲什麼不使用Singleton類來讀取properties文件 然後通過實現ServletContextListener @ contextInitialized方法創建applicationLister並調用你的類的getInstance(這一步不是強制性的,所以你可以把它留下,它是隻是爲了在應用程序容器的起始處實例化單身人士),

Adter在您的整個項目中調用satic方法YouClass.getInstance()並訪問您的屬性。

例如:

public class MyPropertieFileReader { 

    private static MyPropertieFileReader instance = null; 
    private Properties properties; 


    protected MyPropertieFileReader() throws IOException{ 

     properties = new Properties(); 
     properties.load(getClass().getResourceAsStream("path-to-property-file.properties")); 

    } 

    public static MyPropertieFileReader getInstance() { 
     if(instance == null) { 
      try { 
       instance = new TestDataProperties(); 
      } catch (IOException ioe) { 
       ioe.printStackTrace(); 
      } 
     } 
     return instance; 
    } 

    public String getProperty(String key) { 
     return properties.getProperty(key); 
    } 

} 

在您的WS,只需撥打

MyPropertieFileReader.getInstance().getProperty("property-name"); 

希望這將有助於。

相關問題