globalconfig.java

来自「JAVA面向对象编程_孙卫琴yaunma」· Java 代码 · 共 44 行

JAVA
44
字号
package uselocal;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class GlobalConfig {
  private static final ThreadLocal<GlobalConfig> threadConfig=
                                       new ThreadLocal<GlobalConfig>();
  private Properties properties = new Properties();
  private GlobalConfig(){
    try{
      //加载配置信息
      InputStream in=getClass().getResourceAsStream("myapp.properties");
      properties.load(in);
      in.close();
    }catch(IOException e){throw new RuntimeException("加载配置信息失败");}
  }
  public static GlobalConfig getInstance(){
    GlobalConfig config=threadConfig.get();
    if(config==null){
      config=new GlobalConfig();
      threadConfig.set(config);
    }
    return config;
  }

  public Properties getProperties() {
    return properties;
  }
  
  public static void main(String args[]){
     GlobalConfig config=getInstance();
  }
}


/****************************************************
 * 作者:孙卫琴                                     *
 * 来源:<<Java面向对象编程>>                       *
 * 技术支持网址:www.javathinker.org                *
 ***************************************************/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?