<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 17,  comments - 12,  trackbacks - 0

    import java.beans.Introspector;
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.Method;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import org.apache.commons.beanutils.ConvertUtils;
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.XPath;
    import org.dom4j.io.SAXReader;

    public class wyzClassPathXMLApplicationContext {
     
     private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
     private Map<String,Object> sigletons =new HashMap<String,Object>();
     
     
     public wyzClassPathXMLApplicationContext(String filename){
      this.readXML(filename);
      this.instanceBeans();
      this.injectObject();
      
      
      
     }
     private void injectObject() {
      for(BeanDefinition beanDefinition : beanDefines){
       
       Object bean=sigletons.get(beanDefinition.getId());
       if(bean!=null){
       try{
       
        PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
        for(PropertyDefinition propertyDefinition: beanDefinition.getPropertys()){
         for(PropertyDescriptor properdesc :ps){
          if(propertyDefinition.getName().equals(properdesc.getName())){
           Method setter=properdesc.getWriteMethod();
           if(setter!=null){
            Object value =null;
             if(propertyDefinition.getRef()!=null && !"".equals(propertyDefinition.getRef().trim())){
                  value = sigletons.get(propertyDefinition.getRef());

              }else{
                value = ConvertUtils.convert(propertyDefinition.getValue(),properdesc.getPropertyType());
              }
                setter.setAccessible(true);
                setter.invoke(bean, value);
           }
           break;
          }
         }
        
          }
        
       }catch(Exception e){
        e.printStackTrace();
       
        
       
         
        }
        
       }
      }
      
     }
     private void instanceBeans() {
      for(BeanDefinition beanDefinition : beanDefines){
       try{
        if(beanDefinition.getClassName()!= null && !"".equals(beanDefinition.getClassName().trim()))
         sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance());
        
       }catch(Exception e){
        e.printStackTrace();
       }
      }
      
     }
     private void readXML(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("http://ns:beans/ns:bean");//創(chuàng)建/beans/bean的查詢路徑
           
            xsub.setNamespaceURIs(nsMap);//設(shè)置命名空間
            List<Element> beans = xsub.selectNodes(document);//獲取文檔中所有bean節(jié)點(diǎn)
            for(Element element : beans){
                String id = element.attributeValue("id");//獲取id屬性
                String clazz = element.attributeValue("class");//獲得所有class屬性
                BeanDefinition beanDefine = new BeanDefinition(id,clazz);
               
                XPath propertysub = element.createXPath("ns:property");
                propertysub.setNamespaceURIs(nsMap);
                List<Element> propertys = propertysub.selectNodes(element);
                for(Element property: propertys){
                 String propertyname = property.attributeValue("name");
                 String propertyref=property.attributeValue("ref");
                 String propertyvalue=property.attributeValue("value");
                
                 System.out.println(propertyname+propertyref+ propertyvalue);
                
                 PropertyDefinition propertyDefinition = new PropertyDefinition(propertyname,propertyref,propertyvalue);
                 beanDefine.getPropertys().add(propertyDefinition);
                
                
                }
               
                beanDefines.add(beanDefine);
               
            }
           
           }catch(Exception e){
            e.printStackTrace();
           
           }
          
      
     }
     public Object getBean(String beanName){
         return this.sigletons.get(beanName);
        
     }

    }

    posted on 2009-03-31 16:05 asiawang 閱讀(328) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    <2009年3月>
    22232425262728
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234

    常用鏈接

    留言簿(3)

    隨筆檔案

    文章檔案

    友情連接

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    主站蜘蛛池模板: 中国亚洲女人69内射少妇| 亚洲av午夜成人片精品网站 | 久久久久亚洲爆乳少妇无 | 99视频在线看观免费| 亚洲理论电影在线观看| 亚洲13又紧又嫩又水多| 国产精品久久久久免费a∨| 亚洲国产视频网站| 女人张开腿给人桶免费视频 | 午夜免费福利小电影| 精品亚洲国产成AV人片传媒| 99免费观看视频| 亚洲av永久无码嘿嘿嘿| 曰皮全部过程视频免费国产30分钟| 亚洲日韩av无码中文| 亚洲成A人片在线观看无码3D| 一级特黄a免费大片| 四虎永久在线精品免费网址 | 成人永久免费福利视频网站| 国产亚洲精品美女久久久久 | 国产成人免费高清激情视频| 亚洲色大成WWW亚洲女子| 四虎永久精品免费观看| jizz日本免费| 久久亚洲私人国产精品vA| 在线a毛片免费视频观看| 国产精品高清免费网站| 亚洲美女视频一区| 免费v片在线观看无遮挡| 视频免费在线观看| 亚洲一区二区三区播放在线| 四虎在线播放免费永久视频| 久草免费福利视频| 亚洲av永久中文无码精品| 亚洲VA中文字幕不卡无码| 精品久久久久久久久免费影院| 亚洲国产精品VA在线观看麻豆 | 在线观着免费观看国产黄| 中出五十路免费视频| 亚洲一区二区三区高清不卡| 最新黄色免费网站|