如何在Tomcat中读取webapp上下文之外的属性文件 [英] How to read a properties file outside my webapp context in Tomcat
本文介绍了如何在Tomcat中读取webapp上下文之外的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个要求,我的webapp需要从Tomcat webapps目录之外的属性文件中读取,例如从$ CATALINA_HOME / properties / myapp.properties读取。我尝试了几件事,但到目前为止没有运气
I have this requirement that my webapp needs to read from a properties file outside the Tomcat webapps directory, for example from $CATALINA_HOME/properties/myapp.properties. I have tried several things but so far no luck
推荐答案
有多种方法。
- 使用环境变量
- 使用系统属性
- 将其设置为应用程序上下文参数Web.xml
下面是一个示例,显示选项1和选项2
Heres a sample ,that showsOption 1 and Option 2
try {
//Use Any Environmental Variable , here i have used CATALINA_HOME
String propertyHome = System.getenv("CATALINA_HOME");
if(null == propertyHome){
//This is a system property that is passed
// using the -D option in the Tomcat startup script
propertyHome = System.getProperty("PROPERTY_HOME");
}
String filePath= propertyHome+"/properties/myapp.properties";
Properties property = new Properties();
property.load(SystemTest.class.getClassLoader().getResourceAsStream(filePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这篇关于如何在Tomcat中读取webapp上下文之外的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文