我的漫漫程序之旅

          專(zhuān)注于JavaWeb開(kāi)發(fā)
          隨筆 - 39, 文章 - 310, 評(píng)論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          簡(jiǎn)單模擬spring的容器管理bean

          package com.spring;

          import java.io.IOException;
          import java.net.URL;
          import java.util.ArrayList;
          import java.util.HashMap;
          import java.util.List;
          import java.util.Map;

          import org.jdom.Document;
          import org.jdom.Element;
          import org.jdom.JDOMException;
          import org.jdom.input.SAXBuilder;
          import org.jdom.xpath.XPath;

          import com.vo.BeanDefinition;

          /**
           * 簡(jiǎn)單模擬spring的ClassPathXmlApplicationContext spring容器管理bean
           * 
           * 
          @author zdw
           * 
           
          */

          @SuppressWarnings(
          "unchecked")
          public class MyApplicationContext
          {
              
          private List<BeanDefinition> list = new ArrayList<BeanDefinition>();
              
          private Map<String, Object> sigletons = new HashMap<String, Object>();

              
          public MyApplicationContext(String fileName)
              
          {
                  
          this.readXML(fileName);
                  
          try
                  
          {
                      instanceBeans();
                  }

                  
          catch (Exception e)
                  
          {
                      e.printStackTrace();
                  }

              }


              
          /**
               * 實(shí)例化所有bean
               * 
               * 
          @throws InstantiationException
               * 
          @throws IllegalAccessException
               * 
          @throws ClassNotFoundException
               
          */

              
          private void instanceBeans() throws InstantiationException,
                      IllegalAccessException, ClassNotFoundException
              
          {
                  
          for (BeanDefinition bean : list)
                  
          {
                      
          if (null != bean.getClassName() && bean.getClassName().length() > 0)
                          sigletons.put(bean.getId(), Class.forName(bean.getClassName())
                                  .newInstance());
                  }

              }


              
          /**
               * 讀取xml配置
               * 
               * 
          @param fileName
               
          */

              
          public void readXML(String fileName)
              
          {
                  SAXBuilder builder 
          = new SAXBuilder();
                  URL url 
          = this.getClass().getClassLoader().getResource(fileName);
                  
          try
                  
          {
                      Document doc 
          = builder.build(url);
                      XPath xpath 
          = XPath.newInstance("//ns:beans/ns:bean");
                      xpath.addNamespace(
          "ns",
                              
          "http://www.springframework.org/schema/beans");
                      List
          <Element> beans = xpath.selectNodes(doc);
                      
          for (Element bean : beans)
                      
          {
                          String id 
          = bean.getAttributeValue("id");
                          String className 
          = bean.getAttributeValue("class");
                          BeanDefinition beanDefinition 
          = new BeanDefinition(id,
                                  className);
                          list.add(beanDefinition);
                      }


                  }

                  
          catch (JDOMException e)
                  
          {
                      e.printStackTrace();
                  }

                  
          catch (IOException e)
                  
          {
                      e.printStackTrace();
                  }

              }


              
          /**
               * 得到指定id的bean
               * 
               * 
          @param id
               * 
          @return
               
          */

              
          public Object getBean(String id)
              
          {
                  
          return sigletons.get(id);
              }

          }

          測(cè)試:
          @Test
              
          public void testMyUserService()
              
          {
                  MyApplicationContext ctx 
          = new MyApplicationContext("beans.xml");
                  UserService us 
          = (UserService) ctx.getBean("userService");
                  us.print();
              }
          下載地址1
          下載地址2

          posted on 2009-01-22 11:00 々上善若水々 閱讀(1580) 評(píng)論(2)  編輯  收藏

          評(píng)論

          # re: 簡(jiǎn)單模擬spring的容器管理bean  回復(fù)  更多評(píng)論   

          哈哈,簡(jiǎn)單的對(duì)象管理器,你的依賴(lài)呢! 沒(méi)啥用!
          2009-01-26 10:05 | Jack.Wang

          # re: 簡(jiǎn)單模擬spring的容器管理bean[未登錄](méi)  回復(fù)  更多評(píng)論   

          @Jack.Wang
          簡(jiǎn)單便于我們理解spring的原理。
          2009-01-29 09:07 | angel

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 灵山县| 屏东市| 从江县| 合水县| 云阳县| 恩平市| 锦州市| 临高县| 西青区| 平阴县| 济南市| 伊金霍洛旗| 蓝山县| 新蔡县| 鹤壁市| 洪泽县| 林州市| 天台县| 商水县| 遂昌县| 嘉义市| 华亭县| 平安县| 汉寿县| 西乌珠穆沁旗| 陆丰市| 内丘县| 济源市| 澎湖县| 济南市| 彭阳县| 青州市| 岳池县| 吉木乃县| 治县。| 旺苍县| 河北区| 正定县| 盐山县| 东光县| 临夏县|