简体   繁体   English

在战争中访问属性文件

[英]Accessing properties file in a war

I'm trying to access my properties file in a war.我试图在战争中访问我的属性文件。 The code is working, but when i'm exporting the code into a war and use a POST (with an accepted input) using Fiddler, it cannot find the config.properties.代码正在运行,但是当我将代码导出到战争中并使用 Fiddler 使用 POST(带有接受的输入)时,它找不到 config.properties。 (NullPointerException) (空指针异常)

The input and webservice are running correctly.输入和网络服务运行正常。 Just trying to figure out a way to edit my properties while using a war.只是想找出一种在使用战争时编辑我的属性的方法。

Some facts:一些事实:

  • I've made a RetreivePropertiesClass.我做了一个 RetreivePropertiesClass。 This uses:这使用:

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

     /com.webapp/WebContent/WEB-INF/config.properties
  • I'm trying to use the properties data in:我正在尝试使用以下属性数据:

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

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

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

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

I already tried to work out the examples given here and here .我已经尝试计算此处此处给出的示例。 Keeps giving the NullPointerException because the properties file isn't loaded.由于未加载属性文件,因此不断给出 NullPointerException。

Can anyone push me in the right direction?谁能把我推向正确的方向?

Cheers!干杯!

TomCat8 中的空指针

If you are going to load the properties file from the classpath, it must be in the WEB-INF/classes directory inside of your war.如果要从类路径加载属性文件,它必须位于 war 内的WEB-INF/classes目录中。 (Or inside a jar inside of WEB-INF/lib ). (或在WEB-INF/lib内的 jar 内)。 The WEB-INF directory itself is not on the classpath. WEB-INF目录本身不在类路径上。

If you make sure the file ends up as WEB-INF/classes/config.properties your above code should work if you change the getResourceAsStream call to getResourceAsStream("/config.properties")如果您确保该文件以WEB-INF/classes/config.properties结束,那么如果您将getResourceAsStream调用更改为getResourceAsStream("/config.properties")则上述代码应该可以工作

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

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

Source: here来源: 这里

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

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