浏览 2396 次
锁定老帖子 主题:IoC的简易实现
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2007-03-08
java 代码
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-03-08
我晕 重新编辑一下
package com.viwo.bean.factory; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.digester.Digester; import org.apache.log4j.Logger; import com.viwo.bean.config.ConfigBean; import com.viwo.bean.interceptor.InterceptorProxyFactory; import com.viwo.sqlmap.config.SqlMapConfig; /** * A simple IoC Container for beans, manage beans for you. * * @author viwo * */ public class BeanFactory { private final static Logger logger = Logger.getLogger(SqlMapConfig.class); private Map configBeanMap = new HashMap(); private static Map beanFactoryMap = new HashMap(); /** * a static synchronized method to get the Bean from a gived beanId * * @param beanId * @return Object */ public static synchronized Object getBean(String beanId) { logger.info("[viwo]-->[enter getBean]"); BeanFactory beanFactory = getBeanFactory(); Map cfgMap = beanFactory.getConfigBeanMap(); ConfigBean cfgBean = (ConfigBean)cfgMap.get(beanId); if(cfgBean == null) { //if there have not beanId will return null return null; } String transaction = cfgBean.getTransaction(); Object beanObject = null; try { beanObject = Class.forName(cfgBean.getType()).newInstance(); } catch (Exception e) { e.printStackTrace(); } Iterator itProperty = cfgBean.getPropertyMap().keySet().iterator(); Iterator itRef = cfgBean.getRefMap().keySet().iterator(); while(itProperty.hasNext()) { String propertyName = itProperty.next().toString(); String propertyValue = cfgBean.getPropertyMap().get(propertyName).toString(); try { BeanUtils.setProperty(beanObject, propertyName, propertyValue); } catch (Exception e) { e.printStackTrace(); } } while(itRef.hasNext()) { String refName = itRef.next().toString(); String refValue = cfgBean.getRefMap().get(refName).toString(); Object refObject = BeanFactory.getBean(refValue); try { BeanUtils.setProperty(beanObject, refName, refObject); } catch (Exception e) { e.printStackTrace(); } } //System.out.println(cfgBean.getType()); if(transaction!=null&&!transaction.equals("")) { String[] trans = transaction.split(","); beanObject = InterceptorProxyFactory.getInterceptorProxyedObject(beanObject,trans); } return beanObject; } /** * a static synchronized method to get the BeanFactoryConfig * * this method will initialize the properity of BeanFactory * * @return BeanFactory */ private static synchronized BeanFactory getBeanFactory() { BeanFactory beanFactory = null; String path = "bean-config.xml"; if(beanFactoryMap.get(path)==null) { Digester digester = new Digester(); digester.setValidating(false); digester.addObjectCreate("beans", "com.viwo.bean.factory.BeanFactory"); digester.addObjectCreate("beans/bean", "com.viwo.bean.config.ConfigBean"); digester.addSetProperties("beans/bean"); digester.addCallMethod("beans/bean/property", "setProperty", 2); digester.addCallParam("beans/bean/property", 0, "name"); digester.addCallParam("beans/bean/property", 1, "value"); digester.addCallMethod("beans/bean/ref", "setRef", 2); digester.addCallParam("beans/bean/ref", 0, "name"); digester.addCallParam("beans/bean/ref", 1, "value"); digester.addSetNext("beans/bean", "addConfigBean", "com.viwo.bean.config.ConfigBean"); try { beanFactory = (BeanFactory)digester.parse(Thread.currentThread().getContextClassLoader().getResource("/"+path).getFile()); beanFactoryMap.put(path, beanFactory); } catch (Exception e) { System.out.println(e.getMessage()); } } else { beanFactory = (BeanFactory)beanFactoryMap.get(path); } return beanFactory; } public void addConfigBean(ConfigBean configBean) { configBeanMap.put(configBean.getId(), configBean); } public Map getConfigBeanMap() { return configBeanMap; } } |
|
返回顶楼 | |