使用dom4j读取spring配置文件
public class ItcastClassPathXmlApplicationContext {
private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
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<String,String> nsMap = new HashMap<String,String>();
nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间
XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径
xsub.setNamespaceURIs(nsMap);//设置命名空间
List<Element> 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();
}
}
Spring管理Bean的原理
猜你喜欢
转载自guancloud911012.iteye.com/blog/1489343
今日推荐
周排行