繁体   English   中英

在战争中访问属性文件

[英]Accessing properties file in a war

我试图在战争中访问我的属性文件。 代码正在运行,但是当我将代码导出到战争中并使用 Fiddler 使用 POST(带有接受的输入)时,它找不到 config.properties。 (空指针异常)

输入和网络服务运行正常。 只是想找出一种在使用战争时编辑我的属性的方法。

一些事实:

  • 我做了一个 RetreivePropertiesClass。 这使用:

     properties.load(this.getClass() .getResourceAsStream("/WEB-INF/config.properties"));
  • 我的属性文件路径:

     /com.webapp/WebContent/WEB-INF/config.properties
  • 我正在尝试使用以下属性数据:

     String url = RetreiveProperties.getUrl(); String driver = RetreiveProperties.getDriver(); String username = RetreiveProperties.getUsername(); String password = RetreiveProperties.getPassword(); // line below causes the NullPointerException Class.forName(driver);
  • 使用的吸气剂:

     public static String getDriver() { return driver = properties.getProperty("jdbc.driver"); }
  • 部署战争时,属性文件位于:

     webapps\\com.webapp1\\WEB-INF\\config.properties
  • 配置属性:

     jdbc.url = jdbc:postgresql://127.0.0.1:2222/gisdb jdbc.driver = org.postgresql.Driver jdbc.username = postgres jdbc.password = admin

我已经尝试计算此处此处给出的示例。 由于未加载属性文件,因此不断给出 NullPointerException。

谁能把我推向正确的方向?

干杯!

TomCat8 中的空指针

如果要从类路径加载属性文件,它必须位于 war 内的WEB-INF/classes目录中。 (或在WEB-INF/lib内的 jar 内)。 WEB-INF目录本身不在类路径上。

如果您确保该文件以WEB-INF/classes/config.properties结束,那么如果您将getResourceAsStream调用更改为getResourceAsStream("/config.properties")则上述代码应该可以工作

如果方法是静态方法,getClass()方法会失败,并提示“Cannot make a static reference to the non-static method getClass() from the type Object”。

相反,您应该使用CurrentClass.class.getClassLoader().getResourceAsStream。

来源: 这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM