size=small] 通过JFig.jar可以实现读取配置文件,把数据保存到集合中,然后通过代理就可以实现动态加载了.
Collection类
- /**
- * @author 42087743
- * 功能:读取配置文件,将数据存储到hashmap中
- * 说明:为节约版面,省去了set和get方法
- */
- public class CollectionConfig {
- //设置xml文件中的name
- private final static String Config_Section = "collection" ;
- //声明存储数据的hashmap
- private static Map<String,String> hashMap = null ;
- //主要方法,通过JFig读取配置文件
- public static Map load(String config_File){
- JFigLocator jFigLocator = new JFigLocator(config_File);
- JFigIF jFigIF = JFig.getInstance(jFigLocator);
- hashMap = new HashMap<String,String>();
- //存储数据到properties中
- Properties props = jFigIF.getSectionAsProperties(Config_Section);
- Enumeration enumeration = props.keys();
- //迭代数据存储到hashmap中
- while (enumeration.hasMoreElements()){
- String key = (String) enumeration.nextElement();
- String value = (String) props.get(key);
- hashMap.put(key, value);
- }
- return hashMap;
- }
- }
/**
* @author 42087743
* 功能:读取配置文件,将数据存储到hashmap中
* 说明:为节约版面,省去了set和get方法
*/
public class CollectionConfig {
//设置xml文件中的name
private final static String Config_Section = "collection";
//声明存储数据的hashmap
private static Map<String,String> hashMap = null;
//主要方法,通过JFig读取配置文件
public static Map load(String config_File){
JFigLocator jFigLocator = new JFigLocator(config_File);
JFigIF jFigIF = JFig.getInstance(jFigLocator);
hashMap = new HashMap<String,String>();
//存储数据到properties中
Properties props = jFigIF.getSectionAsProperties(Config_Section);
Enumeration enumeration = props.keys();
//迭代数据存储到hashmap中
while(enumeration.hasMoreElements()){
String key = (String) enumeration.nextElement();
String value = (String) props.get(key);
hashMap.put(key, value);
}
return hashMap;
}
}
Proxy类
- /**
- * @author 42087743
- * 功能:代理,为用户屏蔽内部代码,方便用户得到实例
- */
- public class Proxy {
- // 创建对象hashmap,得到读取配置文件的hashmap
- private static Map hashMap = CollectionConfig.getHashMap();
- // 用户输入key,返回给用户value
- public static Object getInstance(String key) throws ClassNotFoundException {
- Object value = null ;
- value = Class.forName(hashMap.get(key).toString());
- return value;
- }
- }
/**
* @author 42087743
* 功能:代理,为用户屏蔽内部代码,方便用户得到实例
*/
public class Proxy {
// 创建对象hashmap,得到读取配置文件的hashmap
private static Map hashMap = CollectionConfig.getHashMap();
// 用户输入key,返回给用户value
public static Object getInstance(String key) throws ClassNotFoundException {
Object value = null;
value = Class.forName(hashMap.get(key).toString());
return value;
}
}
config.xml
- <?xml version= "1.0" encoding= "utf-8" ?>
- <CONFIGURATION>
- <section name="collection" >
- <entry key="java.util.List" value= "java.util.ArrayList" />
- <entry key="java.util.Set" value= "java.util.HashSet" />
- <entry key="java.util.Map" value= "java.util.HashMap" />
- </section>
- </CONFIGURATION>
<?xml version="1.0" encoding="utf-8"?>
<CONFIGURATION>
<section name="collection">
<entry key="java.util.List" value="java.util.ArrayList" />
<entry key="java.util.Set" value="java.util.HashSet" />
<entry key="java.util.Map" value="java.util.HashMap" />
</section>
</CONFIGURATION>
Test类
- /**
- * @author 42087743
- * 功能:测试,读取指定的配置文件,动态加载它,最后运行它的方法
- */
- public class Test {
- public static void main(String[] args) throws ClassNotFoundException,
- InstantiationException, IllegalAccessException,
- IllegalArgumentException, InvocationTargetException {
- String key = "java.util.Map" ;
- // 读取配置文件
- CollectionConfig.load("com/jfig/config.xml" );
- // 加载类
- Class o = (Class) Proxy.getInstance(key);
- // 获得实例
- Object ob = o.newInstance();
- // 得到方法数组
- Method[] method = o.getMethods();
- // 迭代,匹配方法名,最后调用该方法
- for ( int i = 0 ; i < method.length; i++) {
- // 将==先进行比较,可以改善性能,因为==比equals速度快
- if ( "put" == method[i].getName()
- || "put" .equals(method[i].getName())) {
- // 将所有方法名放到集合中去
- for ( int j = 0 ; j < method.length; j++) {
- method[i].invoke(ob,
- new Object[] { j, method[j].toString() });
- }
- // 直接跳出循环,无须继续循环下去
- break ;
- }
- }
- for ( int i = 0 ; i < method.length; i++) {
- if ( "size" == method[i].getName()
- || "size" .equals(method[i].getName())) {
- System.out.println("集合的大小是:" + method[i].invoke(ob));
- break ;
- }
- }
- }
- }
/**
* @author 42087743
* 功能:测试,读取指定的配置文件,动态加载它,最后运行它的方法
*/
public class Test {
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
String key = "java.util.Map";
// 读取配置文件
CollectionConfig.load("com/jfig/config.xml");
// 加载类
Class o = (Class) Proxy.getInstance(key);
// 获得实例
Object ob = o.newInstance();
// 得到方法数组
Method[] method = o.getMethods();
// 迭代,匹配方法名,最后调用该方法
for (int i = 0; i < method.length; i++) {
// 将==先进行比较,可以改善性能,因为==比equals速度快
if ("put" == method[i].getName()
|| "put".equals(method[i].getName())) {
// 将所有方法名放到集合中去
for (int j = 0; j < method.length; j++) {
method[i].invoke(ob,
new Object[] { j, method[j].toString() });
}
// 直接跳出循环,无须继续循环下去
break;
}
}
for (int i = 0; i < method.length; i++) {
if ("size" == method[i].getName()
|| "size".equals(method[i].getName())) {
System.out.println("集合的大小是:" + method[i].invoke(ob));
break;
}
}
}
}
后来想拓展下,进 而实现spring的IOP功能,发现没有那么简单,首先就因为class类型没有办法转型到其他的常用类型,看来还需要继续研究.
[/size]