读取xml配置文件的方法

package com.bluedot.spring; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import com.bluedot.BeanFactory; //读取xml配置文件的方法 public class ClassPathXmlApplicationContext implements BeanFactory { private Map<String,Object> map=new HashMap<String,Object>(); public ClassPathXmlApplicationContext() { } public ClassPathXmlApplicationContext(String fileName) { DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); try { DocumentBuilder db=dbf.newDocumentBuilder(); InputStream is=Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); Document doc=db.parse(is); NodeList beans=doc.getElementsByTagName("bean"); for(int i=0;i<beans.getLength();i++){ Element bean=(Element) beans.item(i); String id=bean.getAttribute("id"); String className=bean.getAttribute("class"); Object value=Class.forName(className).newInstance(); map.put(id, value); } } catch (Exception e) { e.printStackTrace(); } } @Override public Object getBean(String beanName) { // TODO Auto-generated method stub return map.get(beanName); } }

你可能感兴趣的:(xml,exception,bean,String,object,Class)