反射技术的运用、工场设计模式的运用、Config配置文件的读取
项目结构:
testConfig.properties
为配置文件;ConfigValue
为接口类;ConfigValueFactory
工场类;Stringvalue、IntValue、BooleanValue是实现ConfigValue接口的实现类;
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;
// 整数
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;
/**
* 配置文件读取测试
* 利用反射、工厂模式等概念,
* @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"));*/
}
}
运行结果:
在更加实际的项目中,应该做异常处理(String 转 int,String转boolean等)