public class ItcastClassPathXmlApplicationContext
{
private List beanDefines = new
ArrayList();
public ItcastApplicationContext(String
filename){
init(filename);
}
private void init(String
filename){
SAXReader saxReader = new SAXReader();
Document document=null;
try{
URL xmlpath =
this.getClass().getClassLoader().getResource(filename);
document = saxReader.read(xmlpath);
Map nsMap = new HashMap();
nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间,下面要用ns,才能找到指定元素!
XPath xsub =
document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径
xsub.setNamespaceURIs(nsMap);//设置命名空间
List beans = xsub.selectNodes(document);//获取文档下所有bean节点
for(Element element: beans){
String id = element.attributeValue("id");//获取id属性值
String clazz = element.attributeValue("class");
//获取class属性值
BeanDefinition beanDefine = new BeanDefinition(id,
clazz);
beanDefines.add(beanDefine);
}
}catch(Exception e){
e.printStackTrace();
}
}
}