千山鳥飛絕 萬徑人蹤滅
          勤練內功,不斷實踐招數。爭取早日成為武林高手
          依賴注入的方式:
          第一、通過接口注入
          第二、通過構造函數注入,設置對象參數
          第三、通過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 笑口常開、財源滾滾來! 閱讀(238) 評論(0)  編輯  收藏 所屬分類: spring學習
           
          主站蜘蛛池模板: 龙山县| 石柱| 南丰县| 万年县| 汪清县| 于都县| 台中市| 岢岚县| 新闻| 大丰市| 怀集县| 察雅县| 镇宁| 民丰县| 甘肃省| 和田市| 大同市| 无极县| 玛沁县| 鞍山市| 松桃| 镇坪县| 康保县| 新民市| 临城县| 两当县| 宜章县| 广水市| 海安县| 达孜县| 高邮市| 乐山市| 成安县| 平邑县| 阿合奇县| 洞头县| 吴川市| 四子王旗| 平果县| 双辽市| 防城港市|