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");//創建/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);
                     
                      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 閱讀(331) 評論(0)  編輯  收藏

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


          網站導航:
           
          <2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(3)

          隨筆檔案

          文章檔案

          友情連接

          搜索

          •  

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 天门市| 喀喇| 禹州市| 佛坪县| 南江县| 玉环县| 巨鹿县| 荆州市| 根河市| 湖南省| 庆阳市| 新闻| 泽库县| 东光县| 和龙市| 东乡县| 南汇区| 东城区| 张北县| 大荔县| 从化市| 盖州市| 巴马| 仙桃市| 黄大仙区| 景洪市| 乌海市| 井研县| 松原市| 黄石市| 临泽县| 宁化县| 郁南县| 关岭| 南通市| 象山县| 旌德县| 苏州市| 祁门县| 扶余县| 东乡县|