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

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

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

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


    通過內部bean注入,其缺點是內部bean只能被一個外部bean使用
    如下:
    <bean id="personService"
      class="cn.itcast.service.impl.PersonServiceBean">
      <property name="IPersonDao">
             <bean class="cn.itcast.dao.impl.PersonDaoBean"></bean><!--   內部bean注入 -->
      </property>
      
      </bean>
      也可以換為:通過 ref的方式也是可以的,其優點是通過ref參數,可以被多個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>  


    剖析依賴注入的內部機制:通過基本數據類型的向屬性值類型的轉換



    /**
     * 實現的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=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();
        }

       }
      }

     }

     /**
      * 完成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);// 讀取路徑
       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");// 取得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學習
     
    主站蜘蛛池模板: 免费一区二区无码视频在线播放| 久久精品亚洲中文字幕无码麻豆| 亚洲国产无线乱码在线观看 | 波多野结衣免费视频观看| 亚洲av无码久久忘忧草| 希望影院高清免费观看视频| 亚洲第一成年人网站| 免费观看激色视频网站bd | 成人区精品一区二区不卡亚洲| 亚洲成人免费在线观看| 亚洲精品国产第1页| 国产h视频在线观看网站免费| 亚洲一线产区二线产区精华| 100000免费啪啪18免进| 亚洲一级特黄特黄的大片| 中文字幕av无码无卡免费| 亚洲人成网站在线播放2019| 免费黄色网址入口| 国产亚洲福利一区二区免费看 | 亚洲欧美成人一区二区三区| 国产成人精品免费视频大全五级| 羞羞网站免费观看| 亚洲一区二区三区自拍公司| 久久久久国产免费| 亚洲人成影院77777| 日本黄色免费观看| 男女拍拍拍免费视频网站| 亚洲AV福利天堂一区二区三| 国产精品1024永久免费视频| 色偷偷亚洲男人天堂| 亚洲午夜久久久久久噜噜噜| 欧洲一级毛片免费| 黄色一级毛片免费看| 久久精品国产亚洲AV麻豆不卡| 2021久久精品免费观看| 亚洲精华国产精华精华液| 亚洲色偷偷综合亚洲AVYP| 亚洲免费中文字幕| 免费在线观看一区| 亚洲最大的视频网站| 亚洲第一黄片大全|