#接口=实现类
com.kay.struts.dao.UserDAO=com.kay.struts.dao.impl.UserDAOImpl
工厂类:
package com.kay.struts.common;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public final class IocUtil
{
//文件路径
private static final String filePath ="com/kay/struts/common/ioc.properties";
private static Map<Class,Object> map;
/**
* 初始化方法
* @return
*/
private static Map init(){
InputStream input = IocUtil.class.getClassLoader().getResourceAsStream(filePath);
Properties pro = new Properties();
try
{
pro.load(input);
input.close();
//获得key集合
Iterator iter = pro.keySet().iterator();
String key = "";
String value = "";
map = new HashMap<Class,Object>();
while(iter.hasNext()){
key = (String) iter.next();
value = pro.getProperty(key);
Class cla = Class.forName(key);
Object obj = Class.forName(value).newInstance();
map.put(cla, obj);
}
return map;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public static Object lookup(Class cls){
if(map == null){
map = new HashMap<Class,Object>();
init();
}
Object obj = map.get(cls);
if(obj != null){
return obj;
}
return null;
}
}
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public final class IocUtil
{
//文件路径
private static final String filePath ="com/kay/struts/common/ioc.properties";
private static Map<Class,Object> map;
/**
* 初始化方法
* @return
*/
private static Map init(){
InputStream input = IocUtil.class.getClassLoader().getResourceAsStream(filePath);
Properties pro = new Properties();
try
{
pro.load(input);
input.close();
//获得key集合
Iterator iter = pro.keySet().iterator();
String key = "";
String value = "";
map = new HashMap<Class,Object>();
while(iter.hasNext()){
key = (String) iter.next();
value = pro.getProperty(key);
Class cla = Class.forName(key);
Object obj = Class.forName(value).newInstance();
map.put(cla, obj);
}
return map;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public static Object lookup(Class cls){
if(map == null){
map = new HashMap<Class,Object>();
init();
}
Object obj = map.get(cls);
if(obj != null){
return obj;
}
return null;
}
}
UserDAO接口:
package com.kay.struts.dao;
import com.kay.struts.common.exception.DAOException;
import com.kay.struts.vo.User;
public interface UserDAO
{
public User findByNameAndPass(String name,String pass) throws DAOException;
}
import com.kay.struts.common.exception.DAOException;
import com.kay.struts.vo.User;
public interface UserDAO
{
public User findByNameAndPass(String name,String pass) throws DAOException;
}
UserDAO 接口实现类:
package com.kay.struts.dao.impl;
import com.kay.struts.common.exception.DAOException;
import com.kay.struts.dao.UserDAO;
import com.kay.struts.vo.User;
public class UserDAOImpl
implements UserDAO
{
public User findByNameAndPass(String name, String pass)
throws DAOException
{
User user = null;
if(name.equals("tom") && pass.equals("123")){
user = new User();
user.setId(1);
user.setName(name);
user.setPass(pass);
}
return user;
}
}
import com.kay.struts.common.exception.DAOException;
import com.kay.struts.dao.UserDAO;
import com.kay.struts.vo.User;
public class UserDAOImpl
implements UserDAO
{
public User findByNameAndPass(String name, String pass)
throws DAOException
{
User user = null;
if(name.equals("tom") && pass.equals("123")){
user = new User();
user.setId(1);
user.setName(name);
user.setPass(pass);
}
return user;
}
}
调用方法:
UserDAO dao = (UserDAO) IocUtil.lookup(UserDAO.class);