?? factory.java
字號:
package com.hevttc.book.factory;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
/**
* @author linzhen
*
*/
public class Factory {
static Map<String, String> map = null;
// 把配置文件中的信息初始化到map中
static {
if (map == null) {
map = new HashMap<String, String>();
SAXBuilder builder = new SAXBuilder(false);
URL url = Thread.currentThread().getContextClassLoader()
.getResource("DaoConfig.xml");
try {
Document doc = builder.build(url.getPath());
Element root = doc.getRootElement();
List beanlist = root.getChildren("bean");
for (int i = 0; i < beanlist.size(); i++) {
Element bean = (Element) beanlist.get(i);
map.put(bean.getAttributeValue("id"), bean
.getAttributeValue("class"));
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 根據(jù)id獲得實現(xiàn)類
public static Object getImp(String id) {
Object dao = null;
try {
dao = Class.forName(map.get(id)).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return dao;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -