<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é)點
            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 閱讀(329) 評論(0)  編輯  收藏

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


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

    常用鏈接

    留言簿(3)

    隨筆檔案

    文章檔案

    友情連接

    搜索

    •  

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 亚洲国产精品久久人人爱| 亚洲国产高清在线精品一区 | 亚洲综合激情六月婷婷在线观看| 亚洲人成网站在线播放vr| 亚洲一区日韩高清中文字幕亚洲| 久久久久国色av免费看| 日本亚洲欧洲免费天堂午夜看片女人员 | 国产A∨免费精品视频| 337p日本欧洲亚洲大胆人人 | 亚洲av无码乱码国产精品fc2| 成年午夜视频免费观看视频| 无码国产精品一区二区免费虚拟VR| xxxxx免费视频| 91精品国产免费| 国产成人免费在线| 又黄又大的激情视频在线观看免费视频社区在线 | 亚洲人成网站18禁止| 亚洲成人在线电影| 亚洲天堂中文资源| 亚洲无限乱码一二三四区| 亚洲AV无码久久久久网站蜜桃| 亚洲精品夜夜夜妓女网| 亚洲精品无码乱码成人| 久久亚洲精品成人777大小说| 亚洲视频免费一区| 亚洲日韩国产精品乱-久| 亚洲欧洲日产国码久在线| 粉色视频免费入口| 久久av免费天堂小草播放| 很黄很污的网站免费| 4hu四虎最新免费地址| 最新猫咪www免费人成| 国产一区二区三区在线免费| 国产精品亚洲w码日韩中文| 无码乱人伦一区二区亚洲一| 亚洲中文字幕久在线| 久久水蜜桃亚洲AV无码精品| 五月婷婷免费视频| 国产免费AV片在线观看| 欧美a级成人网站免费| 亚洲 自拍 另类小说综合图区|