首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring治理Bean的原理

2012-06-27 
Spring管理Bean的原理使用dom4j读取spring配置文件public class ItcastClassPathXmlApplicationContext {p

Spring管理Bean的原理
使用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();
        }
    }

热点排行