Never give up!

          如果說軟件開發是一條布滿荊棘的坎坷之路,那么我會每天在這道路兩旁書上“火焰舞者,到此一游!”。

           

          Spring IoC的簡單實現1

          先定義一個BeanDefinition
          public class BeanDefinition {

              
          private String id;
              
          private String className;
              
              
          public BeanDefinition(String id, String className) {
                  
          this.id = id;
                  
          this.className = className;
              }

              
              
          public String getId() {
                  
          return id;
              }

              
              
          public void setId(String id) {
                  
          this.id = id;
              }

              
              
          public String getClassName() {
                  
          return className;
              }

              
              
          public void setClassName(String className) {
                  
          this.className = className;
              }

          }

          接著寫IoC的實現容器,流程如下:
          讀取xml配置文件—>把配置文件內所有的bean節點信息添加到BeanDefinition容器內—>構造和BeanDefinition相應的BeanClass—>查找bean
          public class MySpringApplicationContext {

              
          /** bean的信息集合 */
              
          private List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
              
              
          /** bean的class集合 */
              
          private Map<String, Class> beanClass = new HashMap<String, Class>();
              
              
          /**
               * 容器初始化
               * 
               * 
          @param fileName 配置文件(path和name)
               
          */

              
          public MySpringApplicationContext(String fileName) {
                  readXML(fileName);
                  initBeanClass(beanDefinitions);
              }

              
              
          /**
               * 獲取MySpringApplicationContext容器內的bean實例
               * 
               * 
          @param id bean id
               * 
          @return bean實例
               
          */

              
          public Object getBean(String id) {
                  
          try {
                      
          return beanClass.get(id).newInstance();
                  }
           catch (InstantiationException e) {
                      
          throw new RuntimeException(e);
                  }
           catch (IllegalAccessException e) {
                      
          throw new RuntimeException(e);
                  }

              }

              
              
          /**
               * 初始化所有bean的class
               * 
               * 
          @param beanDefinitions bean定義信息
               
          */

              
          private void initBeanClass(List<BeanDefinition> beanDefinitions) {
                  
          for (BeanDefinition beanDefinition : beanDefinitions) {
                      
          if (beanDefinition.getClassName() != null && beanDefinition.getClassName().length() != 0{
                          beanClass.put(beanDefinition.getId(), getClass(beanDefinition));
                      }
           else {
                          
          throw new RuntimeException("class is empty");
                      }

                  }

              }

              
              
          /**
               * 讀取XML文件(利用dom4j,加dom4j-1.6.1.jar、jaxen-1.1-beta-7.jar文件)
               * 
               * 
          @param fileName 配置文件(path和name)
               
          */

              
          private void readXML(String fileName) {
                  SAXReader reader 
          = new SAXReader();
                  Document document 
          = null;
                  URL url 
          = getClass().getClassLoader().getResource(fileName);
                  
                  
          try {
                      document 
          = reader.read(url);
                      
                      Map
          <String, String> nameSpaceMap = new HashMap<String, String>();
                      nameSpaceMap.put(
          "namespace""http://www.springframework.org/schema/beans"); // 加入命名空間
                      
                      XPath xpath 
          = document.createXPath("//namespace:beans/namespace:bean"); // 創建查詢路徑(beans根目錄下的bean元素)
                      xpath.setNamespaceURIs(nameSpaceMap); // 設置命名空間
                      
                      List
          <Element> beans = xpath.selectNodes(document); // 獲取beans根目錄下的所有bean節點
                      for (Element element : beans) {
                           String id 
          = element.attributeValue("id"); // 獲取id屬性值
                           String className = element.attributeValue("class"); // 獲取class屬性值
                           BeanDefinition definition = new BeanDefinition(id, className);
                           beanDefinitions.add(definition);
                      }

                      
                  }
           catch (DocumentException e) {
                      
          throw new RuntimeException("failed to read xml file!", e);
                  }

              }

              
              
          /**
               * 獲取class
               * 
               * 
          @param beanDefinition bean定義信息
               * 
          @return class
               
          */

              
          private Class getClass(BeanDefinition beanDefinition) {
                  
                  
          try {
                      
          return Class.forName(beanDefinition.getClassName());
                  }
           catch (ClassNotFoundException e) {
                      
          throw new RuntimeException("class not found!", e);
                  }

              }

              
          }

          XML文件:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans
              
          xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation
          ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

              
          <bean id="productDao" class="ProductDao">
              
          </bean>
              
          </beans>

          Bean:
          public class ProductDao {

              
          private String name;
              
              
          public String getName() {
                  
          return name;
              }


              
          public void setName(String name) {
                  
          this.name = name;
              }


              
          public void save() {
                  System.out.println(
          "產品插入數據庫");
              }

          }

          測試類: 
          public class SpringTest {

              
          public static void main(String[] args) {
                  MySpringApplicationContext applicationContext 
          = new MySpringApplicationContext("applicationContext.xml");
                  ProductDao productDao 
          = (ProductDao)applicationContext.getBean("productDao");
                  productDao.save();
                  
              }

          }


          測試結果:產品插入數據庫

          posted on 2009-03-26 14:41 永遠的火焰舞者 閱讀(327) 評論(0)  編輯  收藏 所屬分類: spring


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類(10)

          隨筆檔案(9)

          文章檔案(1)

          搜索

          最新評論

          • 1.?re: JForum安裝
          • 我就是想研究下sso,哈哈!再在JForum的基礎上二次開發玩玩 呵呵
          • --Jlg
          • 2.?re: JForum安裝
          • JForum的代碼還比較清晰,但談不上強大,雖然一般也足夠用了。
          • --一農

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 余姚市| 高安市| 高陵县| 天镇县| 峡江县| 凤山县| 南城县| 来安县| 建宁县| 疏附县| 鹤壁市| 尼勒克县| 谷城县| 沽源县| 襄城县| 江门市| 枝江市| 林芝县| 宣武区| 肇东市| 宿州市| 贡山| 巴彦县| 鲜城| 且末县| 灵武市| 越西县| 成武县| 望奎县| 禄丰县| 武川县| 五峰| 象州县| 神木县| 郯城县| 九寨沟县| 酉阳| 广灵县| 利辛县| 海丰县| 太白县|