一晃又是两三个月到此一游了,呵呵,既来之顺手留点印迹吧。
大多数我们在Web服务器上发布应用程序时,都把配置文件都放到包外放置,
也有特例的时候,直接打到War包里的情况切实碰到了,且是在个不太常用的
web服务器(WebOTX,日本NEC自己造的个web服务器),个人感觉这个服务器
是个WebLogic的山寨版呵呵,特别的原因下用了这个特别的软件。
印象中其他Web服务器对包里配置文件也都能支持找到,但这个玩意就是不行,
虽然它也是把包解压到临时目录下,默认寻找目录竟然没有类根目录(也可能是咱没有理解这个玩意的用法),
问题总要解决的啊,借鉴了struts框架里的代码,具体简单的代码如下
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Properties
*/
public class PropFile {
/** props */
private Properties props = null;
/** objProp */
private static PropFile objProp = null;
/**
*
*
* @param key String
* @return
*/
public String getValue(String key) {
return (String) props.get(key);
}
/**
*
*
* @return
*/
public static PropFile getPropObject() {
if (objProp != null) {
return objProp;
}
objProp = new PropFile();
objProp.load();
return objProp;
}
/**
*
*
*/
protected void load() {
if (props != null) {
return;
}
props = new Properties();
InputStream is = null;
ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
//
is = classLoader.getResourceAsStream("xxx.properties");
if (is != null) {
try {
props.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}