size=small]      通过JFig.jar可以实现读取配置文件,把数据保存到集合中,然后通过代理就可以实现动态加载了.
Collection类


Java代码



1. /**
2.  * @author 42087743
3.  * 功能:读取配置文件,将数据存储到hashmap中
4.  * 说明:为节约版面,省去了set和get方法
5.  */
6. public   class
7. //设置xml文件中的name
8. private   final   static  String Config_Section =  "collection"
9. //声明存储数据的hashmap
10. private   static  Map<String,String> hashMap =  null
11. //主要方法,通过JFig读取配置文件
12. public   static
13. new
14.         JFigIF jFigIF = JFig.getInstance(jFigLocator);   
15. new
16.            
17. //存储数据到properties中
18.         Properties props = jFigIF.getSectionAsProperties(Config_Section);   
19.         Enumeration enumeration = props.keys();   
20. //迭代数据存储到hashmap中
21. while
22.             String key = (String) enumeration.nextElement();   
23.             String value = (String) props.get(key);   
24.             hashMap.put(key, value);   
25.         }   
26. return
27.     }   
28. }


/**
 * @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;
	}
}



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Proxy类


Java代码



1. /**
2.  * @author 42087743 
3.  * 功能:代理,为用户屏蔽内部代码,方便用户得到实例
4.  */
5. public   class
6. // 创建对象hashmap,得到读取配置文件的hashmap
7. private   static
8.    
9. // 用户输入key,返回给用户value
10. public   static  Object getInstance(String key)  throws
11. null
12.         value = Class.forName(hashMap.get(key).toString());   
13. return
14.     }   
15. }


/**
 * @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;
	}
}



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config.xml


Java代码


1. <?xml version= "1.0"  encoding= "utf-8"
2. <CONFIGURATION>   
3. "collection"
4. "java.util.List"  value= "java.util.ArrayList"
5. "java.util.Set"  value= "java.util.HashSet"
6. "java.util.Map"  value= "java.util.HashMap"
7.     </section>   
8. </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>
<?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>



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test类


Java代码



1. /**
2.  * @author 42087743 
3.  * 功能:测试,读取指定的配置文件,动态加载它,最后运行它的方法
4.  */
5. public   class
6. public   static   void  main(String[] args)  throws
7.             InstantiationException, IllegalAccessException,   
8.             IllegalArgumentException, InvocationTargetException {   
9. "java.util.Map"
10. // 读取配置文件
11. "com/jfig/config.xml"
12. // 加载类
13.         Class o = (Class) Proxy.getInstance(key);   
14. // 获得实例
15.         Object ob = o.newInstance();   
16. // 得到方法数组
17.         Method[] method = o.getMethods();   
18. // 迭代,匹配方法名,最后调用该方法
19. for  ( int  i =  0
20. // 将==先进行比较,可以改善性能,因为==比equals速度快
21. if  ( "put"
22. "put"
23. // 将所有方法名放到集合中去
24. for  ( int  j =  0
25.                     method[i].invoke(ob,   
26. new
27.                 }   
28. // 直接跳出循环,无须继续循环下去
29. break
30.             }   
31.         }   
32. for  ( int  i =  0
33. if  ( "size"
34. "size"
35. "集合的大小是:"
36. break
37.             }   
38.         }   
39.     }   
40. }


/**
 * @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;
			}
		}
	}
}



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;后来想拓展下,进 而实现spring的IOP功能,发现没有那么简单,首先就因为class类型没有办法转型到其他的常用类型,看来还需要继续研究.
[/size]