简体   繁体   English

Java:访问war中的属性文件

[英]Java: Accessing properties file inside a war

I already searched StackOverflow for "properties inside war", but none of the results worked for my case. 我已经在StackOverflow中搜索了“战争中的属性”,但是没有一个结果适用于我的情况。

I am using Eclipse Galileo and GlassFish v3 to develop a set of web services. 我正在使用Eclipse Galileo和GlassFish v3来开发一组Web服务。 I am using a "dynamic web project" with the following structure 我正在使用具有以下结构的“动态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

I want to access the "configfile.properties" inside one of the source files in "java_code_pkg1". 我想访问“java_code_pkg1”中的一个源文件中的“configfile.properties”。 I am using the Spring Framework and this file will be instantiated once the application starts on the server. 我正在使用Spring Framework,一旦应用程序在服务器上启动,这个文件就会被实例化。

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? 当它不在WEB-INF / classes路径下时是否可以访问文件? 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. 我可以在调试器中看到这些值,但是在执行“props.load”行之后,在进入它下面的灯之前,我得到一个NullPointerException。

That's a different issue. 那是一个不同的问题。 At least now I know this is the way to access the config file. 至少现在我知道这是访问配置文件的方法。

Thank you for your help. 谢谢您的帮助。

If you are in a war, your classpath "current directory" is "WEB-INF/classes". 如果你在战争中,你的类路径“当前目录”是“WEB-INF / classes”。 Simply go up two levels. 简单地上升两个级别。

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

It is horrible but it works. 这太可怕但它确实有效。 At least, it works under tomcat, jboss and geronimo and It works today . 至少,它可以在tomcat,jboss和geronimo下工作,它今天起作用。

PS Your directory structure is not very clear. PS你的目录结构不是很清楚。 Perhaps it is: 也许它是:

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

Check the location of the properties file in WAR file. 检查WAR文件中属性文件的位置。 If it is in WEB-INF/classes directory under com/company/config directory getResourceAsStream("com/company/config/configfile.properties") should work or getResourceAsStream(" This should work if the config file is not under WEB-INF/classes directoy 如果它位于com / company / config目录下的WEB-INF / classes目录中,则getResourceAsStream("com/company/config/configfile.properties")应该工作或getResourceAsStream(“如果配置文件不在WEB-INF下,这应该可以工作/ classes directoy

Also try using getClass().getClassLoader().getResourceAsStream. 还可以尝试使用getClass()。getClassLoader()。getResourceAsStream。

Are you sure the file is being included in your war file? 您确定该文件包含在您的war文件中吗? A lot of times, the war build process will filter out non .class files. 很多时候,war构建过程会过滤掉非.class文件。

What is the path once it is deployed to the server? 一旦部署到服务器,路径是什么? It's possible to use Scanner to manually read in the resource. 可以使用Scanner手动读入资源。 From a java file within a package, creating a new File("../applications/") will get you a file pointed at {glassfish install}\\domains\\{domain name}\\applications. 从包中的java文件,创建一个新文件(“../ applications /”)将获得一个指向{glassfish install} \\ domains \\ {domain name} \\ applications的文件。 Maybe you could alter that file path to direct you to where you need to go? 也许你可以改变那个文件路径,引导你到你需要去的地方?

Since you are using Spring, then use the Resource support in Spring to inject the properties files directly. 由于您使用的是Spring,因此请使用Spring中的Resource支持直接注入属性文件。

see http://static.springsource.org/spring/docs/3.0.x/reference/resources.html 请参阅http://static.springsource.org/spring/docs/3.0.x/reference/resources.html

Even if the class that requires the properties file is not Spring managed, you can still get access to the ApplicationContext and use it to load the resource 即使需要属性文件的类不是Spring管理的,您仍然可以访问ApplicationContext并使用它来加载资源

resource would be something like, classpath:settings.properties, presuming that your properties file got picked up by your build and dropped in the war file. 资源类似于classpath:settings.properties,假设您的属性文件被构建器拾取并放入war文件中。

You can also inject directly, from the docs: 您也可以直接从文档中注入:

<property name="template" value="classpath:some/resource/path/myTemplate.txt">

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

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