---
title: "单例模式取配置文件"
author: "java_cd"
date: "2009-12-09T20:26:14Z"
tags: []
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2009-12-09T20:26:14Z"
id: 545025
url: "https://java-cd.iteye.com/blog/545025"
---

import java.io.FileInputStream;    
import java.io.FileNotFoundException;    
import java.io.IOException;    
import java.io.InputStream;    
import java.util.Properties;    
   
/**   
 * @author JAVA世纪网(java2000.net, laozizhu.com)   
 */   
public class Test {    
  public static void main(String args) {    
  }    
}    
   
/**   
 * @authorleno 单子模式，保证在整个应用期间只加载一次配置属性文件   
 */   
class Singleton {    
  private static Singleton instance;    
   
  private static final String CONFIG_FILE_PATH = "E:\\config.properties";    
   
  private Properties config;    
   
  private Singleton() {    
    config = new Properties();    
    InputStream is;    
    try {    
      is = new FileInputStream(CONFIG_FILE_PATH);    
      config.load(is);    
      is.close();    
    } catch (FileNotFoundException e) {    
      e.printStackTrace();    
    } catch (IOException e) {    
      e.printStackTrace();    
    }    
  }    
   
  public static Singleton getInstance() {    
    if (instance == null) {    
      instance = new Singleton();    
    }    
    return instance;    
  }    
   
  public Properties getConfig() {    
    return config;    
  }    
   
  public void setConfig(Properties config) {    
    this.config = config;    
  }    
}