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

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

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

    千山鳥飛絕 萬徑人蹤滅
    勤練內(nèi)功,不斷實踐招數(shù)。爭取早日成為武林高手
    依賴注入的方式:
    第一、通過接口注入
    第二、通過構(gòu)造函數(shù)注入,設置對象參數(shù)
    第三、通過setter方法注入。
    后兩者運用居多。


    通過內(nèi)部bean注入,其缺點是內(nèi)部bean只能被一個外部bean使用
    如下:
    <bean id="personService"
      class="cn.itcast.service.impl.PersonServiceBean">
      <property name="IPersonDao">
             <bean class="cn.itcast.dao.impl.PersonDaoBean"></bean><!--   內(nèi)部bean注入 -->
      </property>
      
      </bean>
      也可以換為:通過 ref的方式也是可以的,其優(yōu)點是通過ref參數(shù),可以被多個bean引用。

    <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>  


    剖析依賴注入的內(nèi)部機制:通過基本數(shù)據(jù)類型的向?qū)傩灾殿愋偷霓D(zhuǎn)換



    /**
     * 實現(xiàn)的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("構(gòu)造方法 ");
      this.readXml(filename);// 調(diào)用 讀取配置文件 的方法
      this.instanceBeans();// 調(diào)用bean的實例化
      this.injectObject();// 注入對象
     }

     /**
      * 為bean對象的屬性注入值
      */
     private void injectObject() {
      for (BeanDefinition beanDefinition : beanDefines) {
       Object bean = sigletons.get(beanDefinition.getId());
       if (bean != null) {
        // 取得屬性描述 ,是一個數(shù)組
        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=null;
             if(propertyDefinition.getRef()!=null && !"".equals(propertyDefinition.getRef().trim())){
              value = sigletons
                .get(propertyDefinition
                  .getRef());
              
             }else{
              //將配置文件里字符串類型轉(zhuǎn)換為屬性類型的值
              value=ConvertUtils.convert(propertyDefinition.getValue(), properdesc.getPropertyType());
              
             }
             setter.setAccessible(true);// 設置為可訪問
             setter.invoke(bean, value);// 把引用對象注入到屬性
             
            }
            break;
           }
          }
         }
        } catch (Exception e) {
         e.printStackTrace();
        }

       }
      }

     }

     /**
      * 完成bean的實例化
      */
     private void instanceBeans() {
      // System.out.println("bean實例化方法被調(diào)用");
      // 利用反射機制把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文件的方法被調(diào)用了");

      SAXReader saxReader = new SAXReader();// 創(chuàng)建讀取器
      Document document = null;
      try {
       URL xmlpath = this.getClass().getClassLoader()
         .getResource(filename);//取得當前xml文件在本地的位置
       
       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);// 設置命名空間
       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);// 查找節(jié)點
        for (Element property : propertys) {
         String propertyName = property.attributeValue("name");// 取得property的name值
         String propertyref = property.attributeValue("ref");// 取得property的ref值
         String propertyValue = property.attributeValue("value");// 取得property的value值

         PropertyDefinition propertyDefinition = new PropertyDefinition(
           propertyName, propertyref,propertyValue);
         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 id="personService"
      class="cn.itcast.service.impl.PersonServiceBean">
      <property name="IPersonDao" ref="personDaoBean"></property>
      
      <property name="name" value="Itcast"></property>
      <property name="age" value="15"></property>
      </bean>
      <bean id="personDaoBean" class="cn.itcast.dao.impl.PersonDaoBean"></bean>  
      


    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");
      ipersonService.Save();
     // ctx.close();
      // ctx.registerShutdownHook();

     }
    }


    public class PropertyDefinition {
     private String name;
     private String ref;
     private String value;
     public PropertyDefinition(String name, String ref,String value) {
      
      this.name = name;
      this.ref = ref;
      this.value=value;
     }
     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
     public String getRef() {
      return ref;
     }
     public void setRef(String ref) {
      this.ref = ref;
    }
    其他略。

    out:
    Itcast
    15
    這是personDaoBean的Add()方法

    posted on 2009-08-27 15:03 笑口常開、財源滾滾來! 閱讀(237) 評論(0)  編輯  收藏 所屬分類: spring學習
     
    主站蜘蛛池模板: 18禁成人网站免费观看| 亚洲av无码一区二区三区网站| 69视频免费观看l| 少妇亚洲免费精品| 亚洲熟妇AV乱码在线观看| 亚洲成在人天堂在线| 久久精品国产亚洲精品| 日韩一区二区三区免费体验| 222www免费视频| 国产一区二区免费视频| 成年大片免费高清在线看黄| 亚洲女女女同性video| 亚洲欧洲自拍拍偷午夜色| 亚洲AV永久无码区成人网站 | 亚洲国产日韩成人综合天堂| 亚洲第一成年免费网站| 1000部国产成人免费视频| 免费国产黄网站在线观看视频| 最近的2019免费中文字幕| 日产久久强奸免费的看| 亚洲av日韩av永久在线观看| 亚洲一区在线视频观看| 亚洲福利一区二区| 久久精品国产亚洲av高清漫画| 国产亚洲精久久久久久无码| 国产专区一va亚洲v天堂| 亚洲va中文字幕无码| 亚洲av麻豆aⅴ无码电影| 午夜免费福利在线观看| 韩国免费三片在线视频| 夜夜嘿视频免费看| 性做久久久久免费看| 免费看片免费播放| 日本高清免费网站| 国产精品免费看久久久久| 免费看男女下面日出水视频| 国产一区二区三区无码免费| 亚洲男女内射在线播放| 在线播放亚洲第一字幕| 亚洲福利视频一区| 91亚洲精品麻豆|