Java:访问战争中的属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3291398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Accessing properties file inside a war
提问by del.ave
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. I am using a "dynamic web project" with the following structure
我正在使用 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
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.
我想访问“java_code_pkg1”中源文件之一中的“configfile.properties”。我正在使用 Spring 框架,一旦应用程序在服务器上启动,这个文件就会被实例化。
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?
当文件不在 WEB-INF/classes 路径下时是否可以访问它?如果是,那么如何?
Thank you
谢谢
采纳答案by del.ave
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.
感谢您的帮助。
回答by corsiKa
Are you sure the file is being included in your war file? A lot of times, the war build process will filter out non .class files.
您确定该文件已包含在您的 war 文件中吗?很多时候,war 构建过程会过滤掉非 .class 文件。
回答by andcoz
If you are in a war, your classpath "current directory" is "WEB-INF/classes". Simply go up two levels.
如果你在战争中,你的类路径“当前目录”是“WEB-INF/classes”。只需上两个级别。
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 下运行,并且今天可以运行。
P.S. Your directory structure is not very clear. Perhaps it is:
PS 你的目录结构不是很清楚。也许是:
getResourceAsStream("../../com.company.config/configfile.properties");
回答by Dorrene Brown
What is the path once it is deployed to the server? It's possible to use Scanner to manually read in the resource. 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. Maybe you could alter that file path to direct you to where you need to go?
部署到服务器后的路径是什么?可以使用 Scanner 手动读取资源。从包内的 java 文件中,创建一个新的 File("../applications/") 将为您提供一个指向 {glassfish install}\domains\{domain name}\applications 的文件。也许您可以更改该文件路径以将您定向到需要去的地方?
回答by Rajendra
Check the location of the properties file in WAR file.
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
检查属性文件在 WAR 文件中的位置。如果它在 com/company/config 目录
getResourceAsStream("com/company/config/configfile.properties")
下的 WEB-INF/classes 目录应该工作或 getResourceAsStream(" 如果配置文件不在 WEB-INF/classes 目录下,这应该工作
Also try using getClass().getClassLoader().getResourceAsStream.
也可以尝试使用 getClass().getClassLoader().getResourceAsStream。
回答by Al Baker
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">