今天碰到了一个日志被打印两次的问题
Configuration类
/**
* 配置文件信息
* @author Administrator
*
*/
public class Configuration {
private static Logger LOG = Logger.getLogger(Configuration.class);
/**
* 文件路径(备用 当前信息刷新到本地)
*/
public static String filePath = null;
/**
* 键值对
*/
private static Properties prop = null;
/**
* 静态构造函数
*/
static{
if(prop==null){
readConfiguration();
}
}
/**
* 默认构造函数
*
*/
public Configuration(){
if(prop==null){
readConfiguration();
}
}
/**
* prop
* @return
*/
public Properties getProperties(){
return prop;
}
/**
* 获取值
* @param key
* @return
*/
public String getValue(String key){
if(key!=null&&!key.equals("")){
return prop.getProperty(key);
}
return null;
}
/**
* 获取值
* @param key
* @return
*/
public String getValue(String key,String defaultVaule){
if(key!=null&&!key.equals("")){
return prop.getProperty(key,defaultVaule);
}
return null;
}
/**
* 读取配置信息
*
* @return
*/
private static synchronized void readConfiguration() {
/**
* 读取dataexchange.properties文件
*/
InputStream in = null;
Properties p = null;
try {
URL url = DEUtil.class.getResource(Constants.CONFIG_FILE_NAME);
if(url!=null){
filePath = java.net.URLDecoder.decode(url.getPath(),"utf-8");
LOG.info(filePath);
in = url.openStream();
}else{
in = DEUtil.class.getResourceAsStream(Constants.CONFIG_FILE_NAME);
}
p = new Properties();
if(p!=null){
p.load(in);
}else{
throw new NullPointerException();
}
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if(in!=null){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (p != null) {
prop = p;
}else{
throw new NullPointerException();
}
}
/**
* 刷新键值对到本地文件
*
*/
public synchronized void porpertiesFlushBack(){
if(prop!=null){
//文件输出流
OutputStream fos = null;
try {
fos = new FileOutputStream(Configuration.filePath);
//将Properties集合保存到流中
prop.store(fos, "Copyright (c) dlmu 233");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if(fos!=null){
fos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
throw new NullPointerException();
}
}
}
还有一个工具类:DEUtil(贴部分代码)
/**
* 数据交换工具类
*
* @author Administrator
*
*/
public class DEUtil {
/**
* 配置信息
*/
private static Configuration conf = new Configuration();
。。。。。省略部分代码
}
还有一个入口:DataExchangeClient
/**
* 数据交换客户端
* @author Administrator
*
*/
public class Client {
private static Logger LOG = Logger.getLogger(Client .class);
/**
* 配置信息
*/
private Configuration conf = null;
/**
* @param args
* @author Administrator
*/
public static void main(String[] args){
/** 创建客户端实例 */
Client client = new Client ();
client.run();
}
/**
* 初始化函数
*
*/
private void initial(){
this.conf = new Configuration();
}
/**
* 默认构造函数
*
*/
public Client () {
initial();
}
}
整个执行下来会出现打印两次日志 就是红色的地方执行了两次
整个过程都是单线程
暂时还不知道怎么解释(望有人能指点下 谢谢)