反射技術(shù)的運(yùn)用、工場(chǎng)設(shè)計(jì)模式的運(yùn)用、Config配置文件的讀取
項(xiàng)目結(jié)構(gòu):
testConfig.properties
為配置文件魏铅;ConfigValue
為接口類;ConfigValueFactory
工場(chǎng)類录煤;Stringvalue壳澳、IntValue、BooleanValue是實(shí)現(xiàn)ConfigValue接口的實(shí)現(xiàn)類嗤详;
ConfigTest程序入口和讀取配置文件
testConfig.properties清單:
[xhtml] view plaincopy
ip=10.111.14.43
port=4444
name=/u5434/u6167/u6587
islocal=false
ConfigValue.java清單:
[java] view plaincopy
package wuhuiwen.config;
public interface ConfigValue {
public Object getValue(Object value);
}
ConfigValueFactory.java清單:
[java] view plaincopy
package wuhuiwen.config;
public class ConfigValueFactory {
public static ConfigValue getConfigValue(Class<?> className) {
try {
return (ConfigValue) Class.forName(
new StringBuffer().append("wuhuiwen.config.")
.append(
className.getSimpleName().substring(0, 1)
.toUpperCase()).append(
className.getSimpleName().substring(1)).append(
"Value").toString()).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
Stringvalue.java清單:
[java] view plaincopy
package wuhuiwen.config;
// 字符串
public class StringValue implements ConfigValue{
public Object getValue(Object value) {
return value.toString();
}
}
IntValue.java清單:
[java] view plaincopy
package wuhuiwen.config;
// 整數(shù)
public class IntValue implements ConfigValue{
public Object getValue(Object value) {
return Integer.parseInt(value.toString());
}
}
BooleanValue.java清單:
[java] view plaincopy
package wuhuiwen.config;
public class BooleanValue implements ConfigValue {
public Object getValue(Object value) {
return new Boolean(value.toString());
}
}
ConfigTest.java清單:
[java] view plaincopy
package wuhuiwen.config.test;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;
import wuhuiwen.config.ConfigValue;
import wuhuiwen.config.ConfigValueFactory;
public class ConfigTest {
public static String IP;
public static int PORT;
public static String NAME;
public static Boolean ISLOCAL;
/**
* 配置文件讀取測(cè)試
* 利用反射个扰、工廠模式等概念,
* @param args
*/
public static void main(String[] args) {
ConfigTest.readConfig();
System.out.println(IP);
System.out.println(PORT);
System.out.println(NAME);
System.out.println(ISLOCAL);
}
public static void readConfig() {
InputStream inputStream = ConfigTest.class.getClassLoader()
.getResourceAsStream("testConfig.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
Field field[] = ConfigTest.class.getFields();
for (Field f : field) {
System.out.println(f.getType().getSimpleName());
ConfigValue cf = ConfigValueFactory.getConfigValue(f.getType());
Object ov = p.get(f.getName().toLowerCase());
Object ovs = cf.getValue(ov);
try {
f.set(null,ovs);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/*System.out.println("ip:" + p.getProperty("ip") + ",port:"
+ p.getProperty("port") + ",name:" + p.getProperty("name"));*/
}
}
運(yùn)行結(jié)果:
在更加實(shí)際的項(xiàng)目中,應(yīng)該做異常處理(String 轉(zhuǎn) int葱色,String轉(zhuǎn)boolean等)