2014-03-27 36 views
0

我有一個屬性文件url.properties位於maven webapp項目中的src/main/resources文件夾中,並希望從類中讀取它。我該怎麼做?我嘗試使用下面的代碼庫,但給我錯誤。讀取Maven中的屬性文件時出現空指針

的src /主/資源/ url.properties

tax.service.url=http://secdevapp11.gspt.net:8080/istore-tax-service/rest/tax 

TaxClient.java
public static String getTaxServiceUrl() { 
     String taxServiceUrl = ""; 
     try { 
      Properties props = GenericUtils.loadProperties("/url.properties"); 
      log.debug("props = " + props); 
      taxServiceUrl = props.getProperty("tax.service.url"); 
      log.debug("taxServiceUrl = " + taxServiceUrl); 
     } catch (IOException ioe) { 
      log.debug("Failed to read properties file: url.properties"); 
     } 
     return taxServiceUrl; 
    } 

GenericUtils.java
public static Properties loadProperties(String fileName) throws IOException { 
     Properties prop = new Properties(); 
     InputStream in = ClassLoader.getSystemResourceAsStream(fileName); 
     prop.load(in); 
     return prop; 
    } 

的pom.xml
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-war-plugin</artifactId> 
    <version>2.3</version> 
    <configuration> 
     <failOnMissingWebXml>true</failOnMissingWebXml> 
     <nonFilteredFileExtensions> 
      <nonFilteredFileExtension>gif</nonFilteredFileExtension> 
      <nonFilteredFileExtension>ico</nonFilteredFileExtension> 
      <nonFilteredFileExtension>jpg</nonFilteredFileExtension> 
      <nonFilteredFileExtension>png</nonFilteredFileExtension> 
      <nonFilteredFileExtension>pdf</nonFilteredFileExtension> 
     </nonFilteredFileExtensions> 
     <webResources> 
      <resource> 
       <directory>src/main/webapp</directory> 
       <filtering>true</filtering> 
      </resource> 
     </webResources>      
    </configuration> 
</plugin> 

錯誤日誌:

SEVERE: Servlet.service() for servlet [CheckoutPaymentController] in context with path [/istore-mvc2-webapp] threw exception 
java.lang.NullPointerException 
    at java.util.Properties$LineReader.readLine(Properties.java:434) 
    at java.util.Properties.load0(Properties.java:353) 
    at java.util.Properties.load(Properties.java:341) 
    at com.istore.utils.GenericUtils.loadProperties(GenericUtils.java:319) 
    at com.istore.web.service.client.TaxClient.getTaxServiceUrl(TaxClient.java:44) 
    at com.istore.web.service.client.TaxClient.getTaxAmount(TaxClient.java:19) 
    at com.istore.utils.TaxCalculator.getSalesTax(TaxCalculator.java:16) 
    at com.istore.web.controllers.CheckoutPaymentController.getTaxableAmt(CheckoutPaymentController.java:52) 
    at com.istore.web.controllers.CheckoutPaymentController.processRequest(CheckoutPaymentController.java:72) 
    at com.istore.web.controllers.CheckoutPaymentController.doPost(CheckoutPaymentController.java:175) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:724) 
+1

和你的屬性文件放置在哪裏? –

+0

您應該檢查'.getSystemResourceAsStream()'的結果是否爲'null' - 這可能是這種情況 – fge

+0

@fge - 是的,我的getSystemResourceAsStream()返回null。爲什麼是這樣,我可以用什麼來代替? – user2325154

回答

1

以 「/」 關閉您的文件名的前面。您不是在路徑上查找文件,而是要求類加載器按名稱查找資源,並且您的文件被命名爲「url.properties」,而不是「/url.properties」。

+0

是的,我刪除了額外的「/」在我的文件名前,但錯誤仍然存​​在 – user2325154

+0

然後我猜src/main/resources不在你的類路徑中。你如何以及在哪裏部署你的應用程序? – azurefrog

+0

另外,在你做一個maven安裝後,看看你的war文件,是你的屬性文件嗎?如果是這樣,在哪裏? – azurefrog

相關問題