JAVA涂鴉
          關(guān)于JAVA的點點滴滴
          posts - 50,  comments - 689,  trackbacks - 0

          最近公司需要,項目中要用到SpringIbatis。趁著過年好好學(xué)習(xí)學(xué)習(xí)。Ibatis就如同Hibernate一樣的持久層技術(shù),學(xué)習(xí)起來難度不大,但Spring可不一樣,揣著IocDJAOP,四處走紅。學(xué)起來可不容易。就市面上而言,就一本《expert one-on-one J2EE Development without EJB中文版》值得參考,為了生活,再貴也得買。這本書的前五章都是說EJB的不是,從第六章開始進入正題,介紹控制反轉(zhuǎn),以后基本都是說Spring了。

                 可能本人比較愚笨,控制反轉(zhuǎn)弄得不明白。這樣就得上網(wǎng)上找答案了。最后在一個叫Bromonblog上找到的淺顯易懂的答案。下面就是引用他說的話:


          IoCDI

            首先想說說IoCInversion of Control,控制倒轉(zhuǎn))。這是spring的核心,貫穿始終。所謂IoC,對于spring框架來說,就是由spring來負責(zé)控制對象的生命周期和 對象間的關(guān)系。這是什么意思呢,舉個簡單的例子,我們是如何找女朋友的?常見的情況是,我們到處去看哪里有長得漂亮身材又好的mm,然后打聽她們的興趣愛 好、qq號、電話號、ip號、iq………,想辦法認識她們,投其所好送其所要,然后嘿嘿……這個過程是復(fù)雜深奧的,我們必須自己設(shè)計和面對每個環(huán)節(jié)。傳 統(tǒng)的程序開發(fā)也是如此,在一個對象中,如果要使用另外的對象,就必須得到它(自己new一個,或者從JNDI中查詢一個),使用完之后還要將對象銷毀(比 Connection等),對象始終會和其他的接口或類藕合起來。

            那么IoC是如何做的呢?有點像通過婚介找女朋友,在我和女朋友之間引入了一個第三者:婚姻介紹所。婚介管理了很多男男女女的資料,我可以向婚 介提出一個列表,告訴它我想找個什么樣的女朋友,比如長得像李嘉欣,身材像林熙雷,唱歌像周杰倫,速度像卡洛斯,技術(shù)像齊達內(nèi)之類的,然后婚介就會按照我 們的要求,提供一個mm,我們只需要去和她談戀愛、結(jié)婚就行了。簡單明了,如果婚介給我們的人選不符合要求,我們就會拋出異常。整個過程不再由我自己控 制,而是有婚介這樣一個類似容器的機構(gòu)來控制。Spring所倡導(dǎo)的開發(fā)方式就是如此,所有的類都會在spring容器中登記,告訴spring你是個什 么東西,你需要什么東西,然后spring會在系統(tǒng)運行到適當(dāng)?shù)臅r候,把你要的東西主動給你,同時也把你交給其他需要你的東西。所有的類的創(chuàng)建、銷毀都由 spring來控制,也就是說控制對象生存周期的不再是引用它的對象,而是spring。對于某個具體的對象而言,以前是它控制其他對象,現(xiàn)在是所有對象 都被spring控制,所以這叫控制反轉(zhuǎn)。如果你還不明白的話,我決定放棄。

          IoC的一個重點是在系統(tǒng)運行中,動態(tài)的向某個對象提供它所需要的其他對象。這一點是通過DIDependency Injection,依賴注入)來實現(xiàn)的。比如對象A需要操作數(shù)據(jù)庫,以前我們總是要在A中自己編寫代碼來獲得一個Connection對象,有了 spring我們就只需要告訴springA中需要一個Connection,至于這個Connection怎么構(gòu)造,何時構(gòu)造,A不需要知道。在系統(tǒng) 運行時,spring會在適當(dāng)?shù)臅r候制造一個Connection,然后像打針一樣,注射到A當(dāng)中,這樣就完成了對各個對象之間關(guān)系的控制。A需要依賴 Connection才能正常運行,而這個Connection是由spring注入到A中的,依賴注入的名字就這么來的。那么DI是如何實現(xiàn)的呢? Java 1.3之后一個重要特征是反射(reflection),它允許程序在運行的時候動態(tài)的生成對象、執(zhí)行對象的方法、改變對象的屬性,spring就是通過反射來實現(xiàn)注入的。關(guān)于反射的相關(guān)資料請查閱java doc


              我想通過Bromon的介紹,大家對IoCDI都有了比較生動的理解了。再來看看《expert one-on-one J2EE Development without EJB中文版》是怎么解釋這兩個概念的。書上是這么說的:

          IoC是一個很大的概念,可以用不同的方式來實現(xiàn)。主要的實現(xiàn)形式有兩種:

          依賴查找:容器提供回調(diào)接口和上下文環(huán)境給組件。EJBApache Avalon都是使用這種方式。

          依賴注入:組件不做定位查詢,只是提供普通的Java方法讓容器去決定依賴關(guān)系。容器全權(quán)負責(zé)組件的裝配,它會把符合依賴關(guān)系的對象通過JavaBean屬性或者構(gòu)造子傳遞給需要的對象。通過JavaBean屬性注射依賴關(guān)系的做法稱為設(shè)值方法注入(Setter Injection);將依賴關(guān)系作為構(gòu)造子參數(shù)傳入的做法稱為構(gòu)造子注入(Constructor Injection)。

          附圖說明:


          spring_2.jpg

             

          到這里,大家應(yīng)該對IoCDI都有了初步的認識了。其實就Spring來說,就是JavaBeanSpring來管理組裝,表面上看就少了幾個new字,其實就是為了降低耦合度,這也是我們做軟件的目標之一。

          至于Spring是怎樣實現(xiàn)IoC的,expert one-on-one J2EE Development without EJB中文版》第七章“Spring框架介紹”很詳細的列舉了多種方法。說實在,一下子看這么多,我真有點糊涂了。我還是自己寫個Demo熟悉一下大名鼎鼎的Spring吧。

          首先得下載SpringSpring網(wǎng)上有兩種Spring 包一種是spring-framework-1.2.6-with-dependencies.zip,另一種是spring-framework-1.2.6.zip。當(dāng)然最好是下載spring-framework-1.2.6-with-dependencies.zip形式的,因為里面包括了更多的東東。spring-framework-1.2.6-with-dependencies.zip的下載地址是:http://prdownloads.sourceforge.net/springframework/spring-framework-1.2.6-with-dependencies.zip

          下載下來,解壓后,你會發(fā)現(xiàn)里面有很多jar文件。因為剛剛接觸Spring,因此我只需要spring-core.jarspring-framework-1.2.6\dist),將其導(dǎo)入eclipse的構(gòu)建路徑中。另外,log日志是需要的,這也是為了養(yǎng)成良好的編程習(xí)慣。將log4j-1.2.9.jarspring-framework-1.2.6\lib\log4j)和commons-logging.jarspring-framework-1.2.6\lib\jakarta-commons)導(dǎo)入到構(gòu)建路徑中。

          準備就緒,開始寫Demo了。

          我的工程的結(jié)構(gòu)是:

             spring_1.jpg

          1、 log4j.properties代碼:

          log4j.rootLogger=Debug, stdout
          log4j.appender.stdout
          =org.apache.log4j.ConsoleAppender
          log4j.appender.stdout.layout
          =org.apache.log4j.PatternLayout
          log4j.appender.stdout.layout.ConversionPattern
          =%c{1- %m%n

          如何使用Log4j,請看我的另一篇轉(zhuǎn)貼的文章:如何使用Log4J

          2、 HelloBean的代碼:

          package com;

          public class HelloBean {
              
          private String helloworld="Hello!World!";
              
              
          public String getHelloworld() {
                  
          return helloworld;
              }
              
              
          public void setHelloworld(String helloworld) {
                  
          this.helloworld = helloworld;
              }
          }

          這是一個簡單的JavaBean,有個String類型的helloworld屬性,初始值是"Hello!World!" 

          3、 Bean.xml代碼:

          <?xml version="1.0" encoding="GBK"?>
          <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
              
          "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>
             
          <bean id="helloBean" class="com.HelloBean">
                  
          <property name="helloworld">
                      
          <value>Hello!Rick</value>
                  
          </property>
             
          </bean>
          </beans>

                  Spirng重點之一就是配置文件,上面是個相當(dāng)簡單的配置文件,我想大家都應(yīng)該看得懂。最后就是寫應(yīng)用程序了。

          4、 Test的代碼:

          package com;

          import org.springframework.beans.factory.*;
          import org.springframework.beans.factory.xml.XmlBeanFactory;
          import org.springframework.core.io.ClassPathResource;
          import org.springframework.core.io.Resource;

          public class Test {

              
          public static void main(String[] args) {
                  
          //實例化JavaBean,主要是為了比較new對象和依賴注入兩者的區(qū)別
                  HelloBean hellobean=new HelloBean();
                  System.out.println(hellobean.getHelloworld());
                  
                  
          //通過Spring訪問JavaBean組件
                  Resource resource=new ClassPathResource("com/bean.xml");
                  BeanFactory factory
          =new XmlBeanFactory(resource);
                  hellobean
          =(HelloBean)factory.getBean("helloBean");
                  System.out.println(hellobean.getHelloworld());
              }
          }

              這個Demo很好的闡述了Spring的Ioc,其實就Spring而言,就是通過配置文件,讓Spring如同一個管家一樣來管理所有的Bean類。

              Spring的依賴注入相對復(fù)雜一點,主要是明白調(diào)用別的Bean,不是通過實例化對象來調(diào)用,而是告訴Spring,我需要什么Bean,然后Spring再向你的Bean里面注入你所需要的Bean對象。
              接下來說說代碼實現(xiàn),我只是在剛剛的例子上再添加一點東東。
              首先要增加一個HelloImp的接口,這是問什么呢,那你得問Spring,它定的規(guī)矩:JavaBean的實現(xiàn)要有兩個部分,一個接口,一個默認實現(xiàn)。你不照做就不行。
              HelloImp代碼:  
               
          package com;

          public interface HelloImp {
              
          public void getName();
          }

             
              實現(xiàn)HelloImp的Hello代碼:
            
          package com;

          public class Hello implements HelloImp {
              
          public void getName(){
                  System.out.println(
          "Jack");
              }
          }
                   
              接著就是在HelloBean中調(diào)用Hello了。Spring的不同之處也在這體現(xiàn)出來。
          package com;

          public class HelloBean {
              
          private String helloworld="Hello!World!";
              
          private HelloImp hello;  //注意這個私有對象是借口

              
          public String getHelloworld() {
                  
          return helloworld;
              }
              
              
          public void setHelloworld(String helloworld) {
                  
          this.helloworld = helloworld;
              }
              
              
          public void setHello(HelloImp hello) {
                  
          this.hello = hello;
              }
              
            
          public void get(){
                  
          this
          .hello.getName();
             }

          }
              注意字體加粗的地方。

              配置文件也需要增加一點東西:

             
          <?xml version="1.0" encoding="GBK"?>
          <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
              
          "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>
          <!—注意引用的類是具體的類Hello-->
             
          <bean id="myHello" class="com.Hello">
             
          </bean>
             
          <bean id="helloBean" class="com.HelloBean">
                  
          <property name="helloworld">
                      
          <value>Hello!Rick</value>
                  
          </property>
                 
          <property name="hello">
                     
          <ref bean="myHello"></ref>
                 
          </property>

             
          </bean>
          </beans>

              注意字體加粗的部分。

              最后在Test中添加一句hellobean.get();就可以看到結(jié)果了。

          package com;

          import org.springframework.beans.factory.*;
          import org.springframework.beans.factory.xml.XmlBeanFactory;
          import org.springframework.core.io.ClassPathResource;
          import org.springframework.core.io.Resource;

          public class Test {

              
          public static void main(String[] args) {
                  HelloBean hellobean
          =new HelloBean();
                  System.out.println(hellobean.getHelloworld());
                  
                  Resource resource
          =new ClassPathResource("com/bean.xml");
                  BeanFactory factory
          =new XmlBeanFactory(resource);
                  
                  hellobean
          =(HelloBean)factory.getBean("helloBean");
                  System.out.println(hellobean.getHelloworld());
                  hellobean.get();
              }
          }

          到這,Spring的IoC和DI總算有了一定的認識,也算是敲開了Spring的大門了。





          posted on 2006-01-23 15:10 千山鳥飛絕 閱讀(18870) 評論(35)  編輯  收藏 所屬分類: Spring

          FeedBack:
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2006-01-23 16:23 | jelly1982
          IoC給我的感覺是, 真正的實現(xiàn)了叫什么OPC, 還是OCP的原則, 所有的東西, 通過配置文件組裝即可.  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2006-02-28 15:52 | xingxing
          不錯  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2006-06-20 09:25 | cuihuawei
          你的文章,讓我茅塞頓開
          謝  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2006-07-11 17:49 | CC來CC去
          謝了  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2007-04-12 18:24 | 家學(xué)227
          為什么我公司用的SPING 的框架
          在struts里引用的是這些包

          import cn.com.chinabank.framework.action.SpringSupportedAction;
          import cn.com.chinabank.framework.exception.BusinessException;
          import cn.com.chinabank.framework.exception.SystemException;
          然后通過 this.getService("..."); 來得到 類??????
          不太明白  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2007-04-25 23:41 | 易京勇
          好文章,謝謝  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2007-04-28 10:07 | lvq
          不錯的文章 有些比喻很恰當(dāng)  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2007-04-29 20:21 | shu
          niu b  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2007-06-05 16:37 | spell
          恩,這個例子不錯  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式[未登錄]
          2007-07-09 17:15 | z
          有了一個全新的認識 謝謝  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2007-09-20 16:23 | 書亭
          不錯  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2007-10-24 15:00 | dads
          dsfddddddddddddddddddddddddddddd  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2008-05-10 17:05 | fgfd
          dfgdfg  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2008-05-10 17:05 | fgfd
          4545  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2008-05-19 09:41 | lyw
          詮釋得不錯,謝謝了  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2008-06-02 21:24 | Userfriendly
          更多關(guān)于IOC的內(nèi)容可以參考下面
          http://www.martinfowler.com/articles/injection.html

          偶像級人物的文章  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式[未登錄]
          2008-08-25 17:27 | stylm
          真是好東東!!
          頂頂!!
          不能沉了!!!
          還是先備份一下吧!!  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2008-08-26 10:47 | gbs
          不錯,支持!  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2008-09-26 22:12 | wwssss
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2008-10-13 10:38 | Justin
          強悍!  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2008-10-17 22:55 | 毛寅偉
          en ..還可以。。。!我不打擊你!。。要是不行的話。。我會打擊的  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2008-11-26 13:00 | dd-up
          @家學(xué)227
          公司自己開發(fā)的框架吧。  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2008-12-16 13:31 | 爆菜的菜鳥
          通俗易懂,好文章  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式[未登錄]
          2008-12-29 18:25 | hehe
          豁然開朗啊  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2009-02-11 22:44 | 網(wǎng)絡(luò)學(xué)習(xí)者
          對于這方面的學(xué)習(xí),自身感受:去下載視屏java方面最能夠入門的就是
          【北京尚學(xué)堂】這個是最好的,講得最明,如果不懂那么說明就是自己的問題了
          不認真呵呵。。開玩笑的!但是的確要好好的理解。。  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2009-06-21 23:33 | 小惡魔
          真是不錯。。呵呵。。比喻非常恰當(dāng)。。以后跟別人說。也能表述的更清楚一點了。  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2009-07-03 11:00 | 八府巡按
          還不錯,頂一下  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2009-08-26 18:37 | re
          太到位了,UP!!!!!!!  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2009-10-30 15:38 | 大幅度
          人才 “借口”還給你寫出來。你行不行   回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2010-03-22 15:35 | Mavk
          十分不錯!  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2010-05-02 09:53 | nrzj
          寫的真好  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2010-09-25 15:18 | 王海洋
          不錯的文章,對于理解更深刻的定義起到很好的作用,  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2011-12-30 11:37 | fhfg
          ghjhgjhgj  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式
          2012-12-20 10:28 | 八進制
          挺清楚的。
          例子里的類名還需要改進,例如習(xí)慣上一般XxxImpl代表“實現(xiàn)類”而非“接口”。  回復(fù)  更多評論
            
          # re: 輕松學(xué)習(xí)Spring IoC容器和Dependency Injection模式[未登錄]
          2013-04-13 21:14 | Jason
          很好,很容易懂  回復(fù)  更多評論
            

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


          網(wǎng)站導(dǎo)航:
           
          正在閱讀:



          <2009年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          常用鏈接

          留言簿(35)

          隨筆檔案

          文章分類

          文章檔案

          好友的blog

          我的其他blog

          老婆的Blog

          搜索

          •  

          積分與排名

          • 積分 - 776124
          • 排名 - 56

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 北川| 南溪县| 兴山县| 都安| 手游| 札达县| 台东市| 海南省| 锦屏县| 新竹市| 东乡县| 长宁县| 贵州省| 武清区| 新巴尔虎右旗| 于都县| 广东省| 永宁县| 和静县| 博白县| 岑巩县| 东源县| 平罗县| 揭西县| 响水县| 泗洪县| 葫芦岛市| 扶余县| 嘉禾县| 庆城县| 来安县| 宜宾县| 久治县| 新丰县| 阿巴嘎旗| 新密市| 新田县| 临澧县| 辰溪县| 德令哈市| 通道|