DaoFactory的问题!求教!
package org.yeeku.factory;
import org.yeeku.dao.base.Dao;
import org.yeeku.consttool.*;
import org.dom4j.*;
import org.dom4j.io.*;
import java.util.*;
import java.io.*;
public class DaoFactory
{
private Map<String , Dao> daoMap = new HashMap<String , Dao>();
private static DaoFactory df;
private DaoFactory()throws Exception
{
Document doc = new SAXReader().read(new File(ConstantsUtil.realPath + "\\daoContext.xml"));
Element root = doc.getRootElement();
List el = root.elements();
for (Iterator it = el.iterator();it.hasNext() ; )
{
Element em = (Element)it.next();
String id = em.attributeValue("id");
String impl = em.attributeValue("class");
Class implClazz = Class.forName(impl);
Dao d = (Dao)implClazz.newInstance();
daoMap.put(id , d);
}
}
public static DaoFactory instance()throws Exception
{
if (df == null)
{
df = new DaoFactory();
}
return df;
}
public Dao getDao(String id)
{
return daoMap.get(id);
}
}
这段代码是STRUTS2和HIBERNATE里的,但我实在是看不出来他的作用,请各位高手指教!
最好能把每句代码,为什么说一下!好的话,我在加分!多谢!
------解决方案--------------------
//这个好像是自己手动的去读XML文件
package org.yeeku.factory;
import org.yeeku.dao.base.Dao;
import org.yeeku.consttool.*;
import org.dom4j.*;
import org.dom4j.io.*;
import java.util.*;
import java.io.*;
public class DaoFactory
{
private Map <String , Dao> daoMap = new HashMap <String , Dao>();
private static DaoFactory df;
private DaoFactory()throws Exception
{
Document doc = new SAXReader().read(new File(ConstantsUtil.realPath + "\\daoContext.xml")); //读XML文件
Element root = doc.getRootElement(); //得到xml根结点
List el = root.elements(); //得到根结点下的所有元素集合
for (Iterator it = el.iterator();it.hasNext() ; ) //读取某一子结点下的结点
{
Element em = (Element)it.next();
String id = em.attributeValue("id"); //得到属性结点 名为ID
String impl = em.attributeValue("class"); //得到属性结点 名为class
Class implClazz = Class.forName(impl); //加载驱动属性结点 名为class的值
Dao d = (Dao)implClazz.newInstance(); //创建这个类的实例
daoMap.put(id , d); //放入Map中
}
}
public static DaoFactory instance()throws Exception
{
if (df == null)
{
df = new DaoFactory();
}
return df;
}
public Dao getDao(String id)
{
return daoMap.get(id);
}
}
------解决方案--------------------
Java code
package org.yeeku.factory;
import org.yeeku.dao.base.Dao;
import org.yeeku.consttool.*;
import org.dom4j.*;
import org.dom4j.io.*;
import java.util.*;
import java.io.*;
public class DaoFactory
{
private Map <String , Dao> daoMap = new HashMap <String , Dao>();
priv