- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让 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 运行主要方法时,这似乎按预期工作。
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 上下文之外的外部属性文件。有几个原因。例如,多个应用程序将共享来自另一个正在运行的应用程序的配置。 WAR 中的属性文件将非常不切实际。如果有人知道另一种从 WAR 中读取外部属性文件的方法,那也将被慷慨地接受为答案。
最佳答案
访问底层文件系统不受 servlet 规范支持并且不能保证工作,特别是如果访问发生在未分解的 .war
文件中(我假设这就是发生的事情你的情况)。
它使您的应用程序依赖于底层系统——操作系统、服务器等。recommended way是将您的配置打包到 .war
文件中。
如果你必须这样做(我同意外部配置是个好主意),这里有一个更独立于操作系统的方法,使用 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() .如果可以,请使用 Spring 之类的库/框架及其 Resource
抽象。
关于java - 应用程序在 Tomcat 服务器上找不到属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30452658/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!