在 JBoss 中部署的 java servlet 中加载属性文件作为战争
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2015384/
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
Load properties file in a java servlet deployed in JBoss as a war
提问by Carlosfocker
I have a servlet deployed as a war in JBoss 4.0.2. I have a properties file for the deployed application. Where should I put this file? Under the conf directory in the jboss server\default\conf folder? How do I load that properties file in a portable manner?
我在 JBoss 4.0.2 中部署了一个 servlet 作为战争。我有一个已部署应用程序的属性文件。我应该把这个文件放在哪里?jboss server\default\conf文件夹下的conf目录下?如何以可移植的方式加载该属性文件?
采纳答案by Pascal Thivent
To load that properties file in a portablemanner, the best way would be to put it on the classpath of the web application (either in a JAR under WEB-INF/lib/
or under WEB-INF/classes/
or on the app server classpath if you want to be able to edit that file without repackaging your web application) and to use Class#getResourceAsStream(String)
.
要加载在该属性文件可移植的方式,最好的办法是把它放在Web应用程序(的类路径或者以JAR下WEB-INF/lib/
或下WEB-INF/classes/
或在应用服务器类路径中,如果你想能够编辑该文件,但不重新打包您的 Web 应用程序)并使用Class#getResourceAsStream(String)
.
The following code gets an InputStream
for a property file which resides in the same package as the servlet in which the code is executed:
以下代码获取InputStream
一个属性文件,该文件与执行代码的 servlet 位于同一个包中:
InputStream inStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("myfile.properties");
Then, load(InputStream)
it into a Properties
object (skipping Exception handling):
然后,load(InputStream)
它变成一个Properties
对象(跳过异常处理):
Properties props = new Properties();
props.load(inStream);
回答by rsp
The best place to put it is under the web-apps' own doc-root, like "./WEB-INF/myapp.properties", i.e. relative to where the servlet container unpacked your .war
or .ear
file. You can provide the properties file directly in the .war
.
把它最好的地方是在网络应用自己的文档根目录下,如“./WEB-INF/myapp.properties”,即相对于在servlet容器解压您的.war
或.ear
文件。您可以直接在.war
.
The ServletContext
has a method getRealPath(String path)
that returns the actual path in the filesystem. Using the real path you can load it in a Properties
collection.
该ServletContext
有一个方法getRealPath(String path)
,返回在文件系统中的实际路径。使用真实路径,您可以将其加载到Properties
集合中。
UpdateThe code in your comment tries to lookup real path for "/", you should ask for the relative path to your properties file, as in:
更新注释中的代码尝试查找“/”的真实路径,您应该询问属性文件的相对路径,如下所示:
String propertiesFilePath = getServletContext().getRealPath("WEB-INF/application.properties");
Properties props = properties.load(new FileInputStream(propertiesFilePath));
回答by Vinodh Ramasubramanian
If the properties file can be deployed along with the application make it part of your source tree. This will result in the properties file to be in the WEB-INF/classes folder.
如果属性文件可以与应用程序一起部署,请将其作为源代码树的一部分。这将导致属性文件位于 WEB-INF/classes 文件夹中。
This can then be read using
然后可以使用
Properties properties = loadProperties("PropertyFileName.properties", this.getClass());
...
public static Properties loadProperties(String resourceName, Class cl) {
Properties properties = new Properties();
ClassLoader loader = cl.getClassLoader();
try {
InputStream in = loader.getResourceAsStream(resourceName);
if (in != null) {
properties.load(in);
}
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
回答by julius
Just get hold of the servletContext and then
只需掌握 servletContext 然后
InputStream stream = getServletContext().getResourceAsStream("/WEB-INF/log4j.properties");
Properties props = new Properties();
props.load(stream);
This will always work, regardless of whether you deploy a war or exploded war.
无论您是部署战争还是爆发战争,这都将始终有效。