负责加载整个项目的配置信息
技术点:懒汉式单例模式
package com.cici.demo.singleton;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Map.Entry;
import org.apache.log4j.Logger;
public class Configuration {
/*Singleton*/
/*the configuration properties file Path*/
private final static String DEFAULT_CONFIGURATION_FILE_PATH = "/com/cici/demo/migration.properties";
private static Configuration instance = null;
public static Properties props = null;
private final static Logger logger = Logger.getLogger (Configuration.class);
/*Lazy loading*/
//The only way to construct new instance
public static synchronized Configuration getInstance(){
if(instance == null){
props = new Properties();
instance = new Configuration();
try {
props.load(Configuration.class.getResourceAsStream (DEFAULT_CONFIGURATION_FILE_PATH));
} catch (IOException e) {
logger.error(e,e);
}
}
return instance;
}
/*parameter format:xxx=xxx*/
public static synchronized Configuration getInstance(final String[] args){
if(instance==null) {
props = new Properties();
instance = new Configuration();
}
//First load the default config
try{
props.load(Configuration.class.getClass ().getResourceAsStream (DEFAULT_CONFIGURATION_FILE_PATH));
}catch(final IOException e){
logger.error(e,e);
}
// Go through the command line arguments, and override the default config
for (final String arg: args) {
parseArgument (arg);
}
return instance;
}
/*The format of the argument: XX,XX*/
private static void parseArgument(final String argument){
final String[] spligArg = argument.split("=");
if(spligArg.length!=2) {
logger.warn("Command line argument using wrong format"+argument);
}else{
props.put(spligArg[0], spligArg[1]);
}
}
@Override
public String toString(){
String configPart = "";
final Iterator<Entry<Object,Object>> it = props.entrySet().iterator();
while(it.hasNext()){
final Entry<Object,Object> entry = it.next();
configPart+=entry.getKey()+"="+entry.getValue();
if(it.hasNext()){
configPart+=",";
}
}
return "Configurations ["+configPart+"]";
}
/**
* Returns the {@link String} value of a configuration parameter. This is the default value form
* when read from the configuration file.
*
* @param name The name of the value to return
* @return the value from the map
*/
public String getValue (final String name) {
return props.getProperty (name);
}
/**
* Add a value to the configuration map
*
* @param name Name of the new value
* @param value Value to insert
*/
public void putValue (final String name, final Object value) {
props.put (name, value);
}
/**
* Returns a value, converted to {@link Integer}
*
* @param name the name of the configuration value
* @return converted value
*/
public Integer getIntValue (final String name) {
return Integer.parseInt (props.getProperty (name));
}
/**
* Returns a value converted to {@link Long}
*
* @param name the name of the configuration value
* @return converted value
*/
public Long getLongValue (final String name) {
return Long.parseLong (props.getProperty (name));
}
}
要加载的配置文件的路径
/src/com/cici/demo/migration.properties
name=cici
password=cici123love
address=jiangfujiayuan
多线程测试 该单例实例变量
package com.cici.demo.multithread;
import com.cici.demo.singleton.Configuration;
public class TestMuiltyThread {
public static void main(String[] args) {
Th1 t1 = new Th1();
Th2 t2 = new Th2();
Th3 t3 = new Th3();
Th4 t4 = new Th4();
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Th1 extends Thread{
public void run() {
Configuration con = Configuration.getInstance();
System.out.println(Thread.currentThread().getName()+" "+con.props.size());
}
}
class Th2 extends Thread{
@Override
public void run() {
String[] strs = {"f1=lili","f2=titi","f3=mimi"};
Configuration conn = Configuration.getInstance(strs);
System.out.println(Thread.currentThread().getName()+" "+conn.props.size());
}
}
class Th3 extends Thread{
@Override
public void run() {
String[] strs2 = {"f4=lili","f5=titi","f6=mimi"};
Configuration connn = Configuration.getInstance(strs2);
System.out.println(Thread.currentThread().getName()+" "+connn.props.size());
}
}
class Th4 extends Thread{
@Override
public void run(){
Configuration connn = Configuration.getInstance();
connn.putValue("f7", "gogo");
connn.putValue("f8", "momo");
System.out.println(Thread.currentThread().getName()+" "+connn.props.size());
}
}
输出结果
Thread-0 3
Thread-2 6
Thread-1 9
Thread-3 11