daofactory.java

来自「基于j2ee的电子留言系统」· Java 代码 · 共 55 行

JAVA
55
字号
package org.yeeku.factory;

import org.yeeku.dao.*;
import org.yeeku.dao.impl.*;

import org.dom4j.*;
import org.dom4j.io.*;

import java.util.*;
import java.io.*;
/**
 * @author  yeeku.H.lee kongyeeku@163.com
 * @version  1.0
 * <br>Copyright (C), 2005-2008, yeeku.H.Lee
 * <br>This program is protected by copyright laws.
 * <br>Program Name:
 * <br>Date: 
 */
public class DaoFactory
{
    private Map<String , Object> daoMap = new HashMap<String , Object>(); 

    private static DaoFactory df;
	
    private DaoFactory(String path)throws Exception
    {
        Document doc = new SAXReader().read(new File(path + "\\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);
            Object obj = implClazz.newInstance();
            daoMap.put(id , obj);            
        }
    }

    public static DaoFactory instance(String path)throws Exception
    {
        if (df == null)
        {
            df = new DaoFactory(path); 
        }
        return df;
    }

    public Object getDao(String id)
    {
        return daoMap.get(id);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?