将属性文件加载到部署在JBoss中的Java servlet中作为战争 [英] Load properties file in a java servlet deployed in JBoss as a war
问题描述
我在JBoss 4.0.2中部署了一个作为战争的servlet.我有一个已部署的应用程序的属性文件.我应该把这个文件放在哪里?在jboss server \ default \ conf文件夹中的conf目录下?如何以可移植的方式加载该属性文件?
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?
推荐答案
要以便携式的方式加载该属性文件,最好的方法是将其放在Web应用程序的类路径中(在WEB-INF/lib/
或WEB-INF/classes/
下的JAR中,或者在应用服务器类路径上,如果您希望能够在不重新包装Web应用程序的情况下编辑该文件),并使用
To load that properties file in a portable manner, 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)
.
以下代码获取属性文件的InputStream
,该属性文件与执行代码的servlet位于同一包中.
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 inStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("myfile.properties");
然后, Properties
对象(跳过异常处理):
Then, load(InputStream)
it into a Properties
object (skipping Exception handling):
Properties props = new Properties();
props.load(inStream);
这篇关于将属性文件加载到部署在JBoss中的Java servlet中作为战争的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!