应用程序在 Tomcat 服务器上找不到属性文件
Application can't find properties file on Tomcat server
我正在尝试让 Tomcat 加载外部 属性 文件,该文件存在于 WAR 文件的 class 路径之外。
我有一个名为 PropertiesSingleton 的简单 class,它应该将属性文件加载到 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();
}
}
当我通过 IDE.
添加 Env 属性 和 运行 主要方法时,这似乎按预期工作
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}
但是,当我鼓起勇气 WAR 打包此代码并将其 运行 放在我的本地 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 上下文之外的外部属性文件。有几个原因。例如,多个应用程序将共享另一个 运行ning 应用程序的配置。 WAR 中的属性文件将非常不切实际。
如果有人知道另一种从 WAR 读取外部属性文件的方法,那也将被慷慨地接受为答案。
访问底层文件系统不受 servlet 规范支持并且不能保证工作,特别是如果访问发生在未分解的 .war
文件中(我假设这就是你的情况).
它使您的应用程序依赖于底层系统 - OS、服务器等。recommended way 是将您的配置打包到 .war
文件中。
如果你必须这样做(我同意外部配置可能是个好主意),这里有一个更 OS 独立的方法应该可以工作,使用 java.net.URL
和 file: URI scheme:
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()。如果可以,请使用 library/framework 之类的 Spring 及其 Resource
抽象。
我正在尝试让 Tomcat 加载外部 属性 文件,该文件存在于 WAR 文件的 class 路径之外。
我有一个名为 PropertiesSingleton 的简单 class,它应该将属性文件加载到 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();
}
}
当我通过 IDE.
添加 Env 属性 和 运行 主要方法时,这似乎按预期工作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}
但是,当我鼓起勇气 WAR 打包此代码并将其 运行 放在我的本地 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 上下文之外的外部属性文件。有几个原因。例如,多个应用程序将共享另一个 运行ning 应用程序的配置。 WAR 中的属性文件将非常不切实际。 如果有人知道另一种从 WAR 读取外部属性文件的方法,那也将被慷慨地接受为答案。
访问底层文件系统不受 servlet 规范支持并且不能保证工作,特别是如果访问发生在未分解的 .war
文件中(我假设这就是你的情况).
它使您的应用程序依赖于底层系统 - OS、服务器等。recommended way 是将您的配置打包到 .war
文件中。
如果你必须这样做(我同意外部配置可能是个好主意),这里有一个更 OS 独立的方法应该可以工作,使用 java.net.URL
和 file: URI scheme:
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()。如果可以,请使用 library/framework 之类的 Spring 及其 Resource
抽象。