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]