日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

IoC容器和Dependency Injection模式

系統(tǒng) 1764 0
本文轉(zhuǎn)載自 http://www.blogjava.net/rickhunter/articles/29015.html


IoC與DI

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

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

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

??? 我想通過Bromon的介紹,大家對(duì)IoC和DI都有了比較生動(dòng)的理解了。再來看看《expert one-on-one J2EE Development without EJB中文版》是怎么解釋這兩個(gè)概念的。書上是這么說的:

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

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

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

附圖說明:
IoC容器和Dependency Injection模式



??
到這里,大家應(yīng)該對(duì)IoC與DI都有了初步的認(rèn)識(shí)了。其實(shí)就Spring來說,就是JavaBean由Spring來管理組裝,表面上看就少了幾個(gè)new字,其實(shí)就是為了降低耦合度,這也是我們做軟件的目標(biāo)之一。

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

首先得下載Spring。Spring網(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形式的,因?yàn)槔锩姘烁嗟臇|東。spring-framework-1.2.6-with-dependencies.zip的下載地址是:http://prdownloads.sourceforge.net/springframework/spring-framework-1.2.6-with-dependencies.zip。

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

準(zhǔn)備就緒,開始寫Demo了。

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

??? IoC容器和Dependency Injection模式
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,請(qǐng)看我的另一篇轉(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;
??? }
}

這是一個(gè)簡(jiǎn)單的JavaBean,有個(gè)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重點(diǎn)之一就是配置文件,上面是個(gè)相當(dāng)簡(jiǎn)單的配置文件,我想大家都應(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) {
??????? //實(shí)例化JavaBean,主要是為了比較new對(duì)象和依賴注入兩者的區(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());
??? }
}
??? 這個(gè)Demo很好的闡述了Spring的Ioc,其實(shí)就Spring而言,就是通過配置文件,讓Spring如同一個(gè)管家一樣來管理所有的Bean類。

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

public interface HelloImp {
??? public void getName();
}
???
??? 實(shí)現(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;? //注意這個(gè)私有對(duì)象是借口

??? 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();
?? }
}
??? 注意字體加粗的地方。

??? 配置文件也需要增加一點(diǎn)東西:
??
<?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總算有了一定的認(rèn)識(shí),也算是敲開了Spring的大門了。

IoC容器和Dependency Injection模式


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 张家川| 腾冲县| 永德县| 襄垣县| 湾仔区| 宁夏| 广东省| 日土县| 静海县| 苏州市| 凤翔县| 手游| 白水县| 抚松县| 泾源县| 民乐县| 沂南县| 介休市| 城步| 天门市| 临泽县| 葫芦岛市| 都安| 玛曲县| 凉城县| 老河口市| 平顺县| 长治县| 华安县| 铅山县| 松潘县| 西贡区| 金堂县| 德江县| 龙山县| 潜山县| 原平市| 商都县| 南开区| 神木县| 大渡口区|