Java:在战争中访问属性文件 [英] Java: Accessing properties file inside a war

查看:156
本文介绍了Java:在战争中访问属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索StackOverflow的properties inside war,但没有一个结果对我的情况有效。



我使用Eclipse Galileo和GlassFish v3开发套web服务。我使用具有以下结构的动态网络项目。

  Src 
-java_code_pkg_1
-java_code_pkg_2
-com.company.config
--configfile.properties WebContent
-META-INF
-WEB-INF
--log4jProperties
--web .xml
--applicationContext.xml
--app-servlet.xml

我想在java_code_pkg1中的一个源文件中访问configfile.properties。我使用Spring框架,一旦应用程序在服务器上启动,这个文件将被实例化。



我试过下面没有运气

  getResourceAsStream(/ com.company.config / configfile.properties); 
getResourceAsStream(/ com / company / config / configfile.properties);
getResourceAsStream(com / company / config / configfile.properties);
getResourceAsStream(/ configfile.properties);
getResourceAsStream(configfile.properties);

getResourceBundle(..)也没有工作。

是否可以访问不在WEB-INF / classes路径下的文件?

谢谢

解决方案

 属性props = new Properties(); 
props.load(this.getClass()。getResourceAsStream(/ com / company / config / file.properties));

在调试模式下工作。我可以看到调试器中的值,但我得到一个NullPointerException,在执行props.load行后,在进入下面的光之前。



这是一个不同的问题。至少现在我知道这是访问配置文件的方式。



感谢您的帮助。


I already searched StackOverflow for "properties inside war", but none of the results worked for my case.

I am using Eclipse Galileo and GlassFish v3 to develop a set of web services. I am using a "dynamic web project" with the following structure

Src
-java_code_pkg_1
-java_code_pkg_2
-com.company.config
--configfile.properties WebContent
-META-INF
-WEB-INF
--log4jProperties
--web.xml
--applicationContext.xml
--app-servlet.xml

I want to access the "configfile.properties" inside one of the source files in "java_code_pkg1". I am using the Spring Framework and this file will be instantiated once the application starts on the server.

I have tried the following with no luck

getResourceAsStream("/com.company.config/configfile.properties");
getResourceAsStream("/com/company/config/configfile.properties");
getResourceAsStream("com/company/config/configfile.properties");
getResourceAsStream("/configfile.properties");
getResourceAsStream("configfile.properties");

getResourceBundle(..) didn't work either.

Is it possible to access a file when it's not under the WEB-INF/classes path? if so then how?

Thank you

解决方案

Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("/com/company/config/file.properties"));

works when I'm in debug mode. I can see the values in the debugger, but I get a NullPointerException right after executing the "props.load" line and before going into the light below it.

That's a different issue. At least now I know this is the way to access the config file.

Thank you for your help.

这篇关于Java:在战争中访问属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆