我正在尝试让Tomcat加载一个外部属性文件,该文件位于WAR文件的类路径之外。
我有一个名为PropertiesSingleton的简单类,假定该类将属性文件加载到java.util.Properties对象中。
package my.test.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PropertiesSingleton {
private static String filenameProperty = "testprops";
private static IProperties properties; //This is an interface that defines a number of get methods for each property
private final static Logger log = LoggerFactory
.getLogger(PropertiesSingleton.class);
public static void main(String[] args) {
IProperties props = PropertiesSingleton.load();
}
public static synchronized IProperties load() {
InputStream stream;
if (properties == null) {
try {
String propertyFileName = System.getProperty(filenameProperty);
String variableFileName = System.getenv(filenameProperty);
log.debug("Value of "+filenameProperty+" parameter: "+propertyFileName);
log.debug("Value of "+filenameProperty+" env variable: "+variableFileName);
PropertiesImpl props = new PropertiesImpl(); //A subclass of java.util.Properties implementing the IProperties interface
if(propertyFileName != null && !propertyFileName.isEmpty()){
File file = new File(propertyFileName);
log.debug("Loading properties from parameter '"+propertyFileName+"'" + (file.exists() ? "" : ", which does not exist") );
stream = new FileInputStream(file);
}else if(variableFileName != null && !variableFileName.isEmpty()){
File file = new File(variableFileName);
log.debug("Loading properties from environment variable'"+variableFileName+"'"+ (file.exists() ? "" : ", which does not exist"));
stream = new FileInputStream(file);
}else{
log.debug("Loading properties from properties file");
stream = ClassLoader.getSystemClassLoader().getResourceAsStream("my/test/properties/test.properties");
}
if(stream == null){
log.warn("Could not load the properties file");
return props;
}
props.load(stream);
properties = props;
log.info("Loaded properties " + properties);
} catch (IOException ex) {
log.error("Could not load properties", ex);
} catch (Exception ex) {
log.error("Unexpected Error: Could not load properties", ex);
throw ex;
}
}
return properties;
}
public static synchronized IProperties reload(){
properties = null;
return load();
}
}
当我添加Env属性并通过IDE运行main方法时,这似乎按预期工作。
2015-05-26 09:17:02,136 DEBUG Value of testprops parameter: null
2015-05-26 09:17:02,139 DEBUG Value of testprops env variable: C:\test\test.properties
2015-05-26 09:17:02,141 DEBUG Loading properties from environment variable'C:\test\test.properties'
2015-05-26 09:17:02,141 INFO Loaded properties {defaultLocale=dk, host=123.123.123.123}
但是,当我不愿将这些代码打包并在本地Tomcat8上运行时,我会收到一条令人振奋的新消息。(tomcat WAR有时会调用PropertiesSingleton.load):
2015-05-26 09:17:02,136 DEBUG Value of testprops parameter: null
2015-05-26 09:17:02,139 DEBUG Value of testprops env variable: C:\test\test.properties
2015-05-26 09:17:02,141 DEBUG Loading properties from environment variable'C:\test\test.properties', which does not exist
2015-05-26 09:42:36,114 ERROR Could not load properties
java.io.FileNotFoundException: C:\test\test.properties (The system cannot find the file specified)
Tomcat中是否有一些安全功能现在允许我访问文件系统,还是其他严重错误?
我还应注意,我正在执行此练习,因为我需要一个外部属性文件,该文件位于Tomcat服务器上WAR上下文之外。有几个原因。例如,几个应用程序将共享另一个正在运行的应用程序的配置。WAR中的属性文件将是不切实际的。如果有人知道从WAR读取外部属性文件的另一种方法,那么该方法也将很乐意被接受为答案。
servlet规范不支持访问基础文件系统,并且不能保证其正常工作,特别是如果访问是通过未爆炸的.war
-file文件进行的(我假设这就是您的情况)。
它使您的应用依赖于基础系统-操作系统,服务器等。建议的方法是将配置打包在.war
-file中。
如果您必须这样做(并且我同意使用外部配置可能是个好主意),那么这是一种更独立于操作系统的方式,应该使用java.net.URL
和,并使用以下文件:URI方案:
URL url = new URL("file:///test/test.properties");
URLConnection con = url.openConnection();
ResourceUtils.useCachesIfNecessary(con);
InputStream stream = con.getInputStream();
改编自org.springframework.core.io.UrlResource.getInputStream()。如果可以,请使用像Spring及其Resource
抽象之类的库/框架。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句