java单例读取配置文件

来源:互联网 发布:商超网络信息平台 编辑:程序博客网 时间:2024/06/01 20:26

源代码取自:云之讯短信接口类的api demo

import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Iterator;import java.util.Properties;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;/** * 单例模式,加载配置文件 * @author dhh * */public class SysConfig{private Properties props = null;  //messageConfig.propertiesprivate static Logger log = Logger.getLogger(SysConfig.class);/** * synchronized 同步块大家都比较熟悉,通过 synchronized 关键字来实现,所有加上synchronized块语句,在多线程访问的时候, * 同一时刻只能有一个线程能够用synchronized 修饰的方 或者代码块。 * volatile修饰的变量,线程在每次使用变量的时候,都会读取变量修改后的最的值。volatile很容易被误用,用来进行原子性操作。 */private static volatile SysConfig conf;private SysConfig() {props = new Properties();loadConfigProps();}public static SysConfig getInstance() {if (conf == null) {synchronized (SysConfig.class) {if (conf == null) {conf = new SysConfig();}}}return conf;}public void loadConfigProps() {InputStream is = null;try {is = getClass().getResourceAsStream("/WEB-INF/classes/messageConfig.properties");if (is == null) {is = getClass().getResourceAsStream("/messageConfig.properties");}InputStreamReader reader = new InputStreamReader(is, "UTF-8");props.load(reader);Iterator<String> iter = props.stringPropertyNames().iterator();while (iter.hasNext()) {String key = iter.next();props.setProperty(key, props.getProperty(key));}} catch (IOException e) {e.printStackTrace();log.error("load config.properties error!please check the file!", e);} finally {if (is != null) {try {is.close();is = null;} catch (Exception e) {e.printStackTrace();}}}}public String getProperty(String key) {String tmp = props.getProperty(key);if (StringUtils.isNotEmpty(tmp)) {return tmp.trim();}return tmp;}public String getProperty(String key, String defaultValue) {String tmp = props.getProperty(key, defaultValue);if (StringUtils.isNotEmpty(tmp)) {return tmp.trim();}return tmp;}public int getPropertyInt(String key) {String tmp = props.getProperty(key);if (StringUtils.isNotEmpty(tmp)) {return Integer.parseInt(tmp.trim());}return 0;}public int getPropertyInt(String key, int defaultValue) {String tmp = props.getProperty(key);if (StringUtils.isNotEmpty(tmp)) {return Integer.parseInt(tmp.trim());}return defaultValue;}public long getPropertyLong(String key, long defaultValue) {String tmp = props.getProperty(key);if (StringUtils.isNotEmpty(tmp)) {return Integer.parseInt(tmp.trim());}return defaultValue;}}

调用代码:

public String version=SysConfig.getInstance().getProperty("version");

我之前用的加载配置文件的代码:

import java.io.IOException;import java.util.Properties;public class PropertiesReader {private static Properties properties = new Properties();static{try {properties.load(PropertiesReader.class.getClassLoader().getResourceAsStream("setting.properties"));} catch (IOException e) {e.printStackTrace();}}public static String readUrl(String key){return (String)properties.get(key);}}

调用:

String api_key = PropertiesReader.readUrl("api_key")


0 0
原创粉丝点击