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

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

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

    千山鳥飛絕 萬徑人蹤滅
    勤練內功,不斷實踐招數。爭取早日成為武林高手
             今天花費了半天去寫自定義容器,結果發現還是沒發實現功能,無法輸出,結果不斷的調試、測試。最后終于搞定了。原來是寫代碼的時候把讀取配置文件里方法readxml,里面讀取子屬性里面查找節點,調用查找對象搞錯了。本為propertysub.selectNodes(element),但寫為了xsub.selectNodes(element)誤用為主element里面查找子節點。還有就是在//ns:beans/ns:bean里面把冒號寫成了頓號,真是太不仔細了。

    自定義容器

    /**
     * 實現的spring容器
     *
     * @author Administrator
     *
     */
    public class ItcastClassPathXMLApplicationContext {

     private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
     private Map<String, Object> sigletons = new HashMap<String, Object>();

     public ItcastClassPathXMLApplicationContext() {

     }

     public ItcastClassPathXMLApplicationContext(String filename) {
      // System.out.println("構造方法 ");
      this.readXml(filename);// 調用 讀取配置文件 的方法
      this.instanceBeans();// 調用bean的實例化
      this.injectObject();// 注入對象
     }

     /**
      * 為bean對象的屬性注入值
      */
     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();// 獲取屬性的setter方法.
            // private
            if (setter != null) {
             Object value = sigletons
               .get(propertyDefinition.getRef());
             setter.setAccessible(true);// 設置為可訪問
             setter.invoke(bean, value);// 把引用對象注入到屬性
            }
            break;
           }
          }
         }
        } catch (Exception e) {
         e.printStackTrace();
        }

       }
      }

     }

     /**
      * 完成bean的實例化
      */
     private void instanceBeans() {
      // System.out.println("bean實例化方法被調用");
      // 利用反射機制把bean實例化
      for (BeanDefinition beanDefinition : beanDefines) {
       try {
        // 判斷BeanDefinition的實例獲得的類名不為null和空串
        if (beanDefinition.getClassName() != null
          && !"".equals(beanDefinition.getClassName().trim()))
         sigletons.put(beanDefinition.getId(), Class.forName(
           beanDefinition.getClassName()).newInstance());
       } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }

      }

     }

     /**
      * 讀取配置文件信息
      *
      * @param filename
      */
     private void readXml(String filename) {
      // System.out.println("讀取xml文件的方法被調用了");

      SAXReader saxReader = new SAXReader();// 創建讀取器
      Document document = null;
      try {
       URL xmlpath = this.getClass().getClassLoader()
         .getResource(filename);//取得當前xml文件在本地的位置
       
       document = saxReader.read(xmlpath);// 讀取路徑
       System.out.println(document);
       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節點
       System.out.println(beans.size());
       for (Element element : beans) {
        String id = element.attributeValue("id");// 獲取id屬性值
        String clazz = element.attributeValue("class");// 獲取class屬性值
        BeanDefinition beanDefine = new BeanDefinition(id, clazz);
        System.out.println("id=" + id);
        System.out.println("clazz=" + 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");// 取得property的name值
         String propertyref = property.attributeValue("ref");// 取得property的ref值

         System.out.println(propertyName + "=  " + propertyref);

         PropertyDefinition propertyDefinition = new PropertyDefinition(
           propertyName, propertyref);
         beanDefine.getPropertys().add(propertyDefinition);// 將屬性對象加入到bean中
        }

        beanDefines.add(beanDefine);
       }
      } catch (Exception e) {
       e.printStackTrace();

      }

     }

     /**
      * 獲取bean 實例
      *
      * @param beanName
      * @return
      */
     public Object getBean(String beanName) {

      
      return this.sigletons.get(beanName);
     }

    }

     bean.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"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
      >
     <bean id="personService"
      class="cn.itcast.service.impl.PersonServiceBean">
      <property name="IPersonDao" ref="personDaoBean"></property>
      
      </bean>
      <bean id="personDaoBean" class="cn.itcast.dao.impl.PersonDaoBean"></bean>  
    </beans>


    自定義屬性類 PropertyDefinition.java

    package junit.test;

    public class PropertyDefinition {
     private String name;
     private String ref;
     public PropertyDefinition(String name, String ref) {
      
      this.name = name;
      this.ref = ref;
     }
      getter&setter method

    }


    測試類:springTest


    package junit.test;

    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;

    import cn.itcast.service.IPersonService;
    import cn.itcast.service.impl.PersonServiceBean;

    public class SpringTest {

     @BeforeClass
     public static void setUpBeforeClass() throws Exception {

     }
     @Test
     public void instanceSpring() {
    //  ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
    //    "beans.xml");
      ItcastClassPathXMLApplicationContext ctx=new ItcastClassPathXMLApplicationContext("beans.xml");
      
      IPersonService ipersonService = (IPersonService)ctx
        .getBean("personService");//調用自定義容器的getBean方法
      ipersonService.Save();
    //  ctx.close();
      // ctx.registerShutdownHook();

     }
    }

    自定義bean類:

    package junit.test;

    import java.util.ArrayList;
    import java.util.List;

    public class BeanDefinition {
     
     private String id;
     private String className;
     private List<PropertyDefinition> propertys=new ArrayList<PropertyDefinition>();
     生成getter,setter方法
     }



    package cn.itcast.dao.impl;

    import cn.itcast.dao.IPersonDao;

    public class PersonDaoBean implements IPersonDao {
     public void add(){
      System.out.println("這是personDaoBean的Add()方法");
     }
    }




    package cn.itcast.service;

    public interface IPersonService {

     public abstract void Save();

    }




    package cn.itcast.service.impl;


    import cn.itcast.dao.IPersonDao;
    import cn.itcast.service.IPersonService;
    /**
     * 業務bean
     * @author Administrator
     *
     */
    public class PersonServiceBean implements IPersonService {

     private IPersonDao iPersonDao;
     
     public IPersonDao getIPersonDao() {
      return iPersonDao;
     }

     public void setIPersonDao(IPersonDao personDao) {
      iPersonDao = personDao;
     }

     public void Save(){
      iPersonDao.add();
     }
     
     
    }



    運行測試類
    out:
     這是personDaoBean的Add()方法


    posted on 2009-08-27 14:46 笑口常開、財源滾滾來! 閱讀(153) 評論(0)  編輯  收藏 所屬分類: spring學習
     
    主站蜘蛛池模板: 日本免费一本天堂在线| 4399影视免费观看高清直播| 日韩一级免费视频| 亚洲日韩一区二区三区| 一本无码人妻在中文字幕免费| 亚洲人6666成人观看| 思思re热免费精品视频66| 亚洲第一页在线播放| 手机在线看永久av片免费| 中文字幕亚洲情99在线| 暖暖免费高清日本一区二区三区| 亚洲欧美国产日韩av野草社区| 国产美女做a免费视频软件| 美女被暴羞羞免费视频| 亚洲欧洲久久久精品| 黄桃AV无码免费一区二区三区| 久久国产精品亚洲综合| 国产精彩免费视频| 亚洲中文字幕无码久久| 国产乱子伦精品免费无码专区| 亚洲精品国产日韩无码AV永久免费网| 亚洲色欲一区二区三区在线观看| 日本免费在线观看| 亚洲a∨无码男人的天堂| 国产免费人成视频在线观看 | 日日躁狠狠躁狠狠爱免费视频| 国产精品亚洲mnbav网站 | 在线观看亚洲人成网站| 99精品全国免费观看视频| 猫咪www免费人成网站| 亚洲AV永久纯肉无码精品动漫| 国产成人精品免费视频大全麻豆| 亚洲欧美日韩综合久久久久| 青青草原亚洲视频| 国产成人精品免费视| 无码人妻一区二区三区免费视频| 国产亚洲精AA在线观看SEE| 丁香花免费完整高清观看 | 亚洲精品免费观看| 日韩高清在线免费看| 男的把j放进女人下面视频免费|