軟件開發(fā)架構(gòu)平臺(tái)技術(shù):Spring IOC_第1頁
軟件開發(fā)架構(gòu)平臺(tái)技術(shù):Spring IOC_第2頁
軟件開發(fā)架構(gòu)平臺(tái)技術(shù):Spring IOC_第3頁
軟件開發(fā)架構(gòu)平臺(tái)技術(shù):Spring IOC_第4頁
軟件開發(fā)架構(gòu)平臺(tái)技術(shù):Spring IOC_第5頁
已閱讀5頁,還剩24頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

軟件開發(fā)架構(gòu)平臺(tái)技術(shù)CH12Spring&IOC回顧持久化框架(ORM)的概念MyBatis框架Hibernate框架目錄Spring框架簡(jiǎn)介控制反轉(zhuǎn)和依賴注入使用IOC進(jìn)行Web應(yīng)用開發(fā)Spring簡(jiǎn)介Spring

:Spring是一個(gè)開源框架,于2003年興起的一個(gè)輕量級(jí)的Java開發(fā)框架。Spring的用途不僅限于服務(wù)器端的開發(fā)。從簡(jiǎn)單性、可測(cè)試性和松耦合的角度而言,任何Java應(yīng)用都可以從Spring中受益。目標(biāo):使現(xiàn)有技術(shù)更加易用,推進(jìn)編碼最佳實(shí)踐。內(nèi)容:依賴注入容器,AOP實(shí)現(xiàn)(聲明式事務(wù)),DAO/ORM支持,Web集成。Spring之父RodJohnsonSpring框架創(chuàng)始人,interface21公司CEO。豐富的c/c++開發(fā)和金融行業(yè)背景。1996年開始關(guān)注Java服務(wù)器端技術(shù),Servlet2.4和JDO2.0專家組成員。2002年著寫《Expertone-on-oneJ2EEDevelopmentwithoutEJB》,對(duì)企業(yè)級(jí)應(yīng)用開發(fā),特別是Java領(lǐng)域。核心理念:Don’tReinventtheWheel.依賴注入的來源復(fù)雜的軟件系統(tǒng)面向?qū)ο笫瓜到y(tǒng)的實(shí)現(xiàn)變得簡(jiǎn)單當(dāng)系統(tǒng)復(fù)雜到一定程度時(shí),僅僅面向?qū)ο蟛荒芙档蛷?fù)雜性依賴注入的來源組件化核心思想:解耦合,將組件的構(gòu)建和組件的使用分開,實(shí)現(xiàn)每個(gè)組件塊時(shí)只考慮組件內(nèi)部的事情,要點(diǎn)是明確和定義組件間的接口。組件的使用組件的生產(chǎn)接口的定義運(yùn)行時(shí)注入依賴注入——案例分析比如要完成一個(gè)小項(xiàng)目,這個(gè)項(xiàng)目的功能是在用戶界面展現(xiàn)一個(gè)字符串(如Helloworld)。這個(gè)項(xiàng)目至少包含3個(gè)功能模塊:從持久數(shù)據(jù)(DB)中讀取字符串;對(duì)讀取的數(shù)據(jù)進(jìn)行處理(編碼、格式等);將數(shù)據(jù)展現(xiàn)在用戶界面(命令行、GUI、HTML)。依賴注入——案例分析方法一:最基本的面向?qū)ο蟮膶?shí)現(xiàn)方式。針對(duì)三個(gè)功能模塊分別編寫三個(gè)類:FileHelloStr類:從持久化數(shù)據(jù)(屬性文件)讀取信息。HelloWorld類:業(yè)務(wù)邏輯,添加Software信息。HelloWorldClient類:將信息輸出到命令行。依賴注入——案例分析方法一:FileHelloStr類public

classFileHelloStr{

privateStringpropfilename;

publicFileHelloStr(Stringpropfilename){

this.propfilename=propfilename;}

publicStringgetContent(){Stringhelloworld="";

try{Propertiesproperties=newProperties(); InputStreamis= getClass().getClassLoader().getResourceAsStream(propfilename);properties.load(is);is.close();helloworld=properties.getProperty(“helloworld"); }catch(FileNotFoundExceptionex){ ex.printStackTrace(); }catch(IOExceptionex){ ex.printStackTrace(); }

returnhelloworld;}}依賴注入——案例分析方法一:HelloWorld類public

classHelloWorld{

publicStringgetContent(){FileHelloStrfhStr=newFileHelloStr("perties");Stringhellworld=fhStr.getContent()+"Software";

returnhellworld;}}方法一:HelloWorldClient類public

classHelloWorldClient{

public

static

voidmain(String[]args){HelloWorldhw=newHelloWorld();System.out.println(hw.getContent());}}依賴注入——案例分析方法一分析HelloWorld類依賴于FileHelloStr類HelloWorldClient類依賴于HelloWorld類系統(tǒng)的各部分之間相互依賴,導(dǎo)致可維護(hù)性和擴(kuò)展性差,不適合大型、企業(yè)級(jí)的應(yīng)用開發(fā)。解決方案用面向接口編程的思想,將業(yè)務(wù)邏輯層(HelloWorld)和DAO層(FileHelloStr)的耦合消除。將HelloWorld類中對(duì)具體的FileHelloStr類的操作,委托給外部類(HelloWorldClient)。依賴注入——案例分析方法二:FileHelloStr類和HelloStr接口public

interface

HelloStr{

publicStringgetContent();}public

interface

HelloStr{

publicStringgetContent();}public

class

FileHelloStrimplementsHelloStr{

privateStringpropfilename;

publicFileHelloStr(Stringpropfilename){

this.propfilename=propfilename;}

publicStringgetContent(){Stringhelloworld="";...... //具體代碼如方法一

returnhelloworld;}}依賴注入——案例分析方法二:HelloWorld類public

classHelloWorld{

privateHelloStrhStr;

publicHelloWorld(HelloStrhStr){

this.hStr=hStr;}

publicStringgetContent(){

return

hStr.getContent()+"Software";}}方法二:HelloWorldClient類public

classHelloWorldClient{

public

static

voidmain(String[]args){HelloStrfhStr=newFileHelloStr("perties");HelloWorldhw=newHelloWorld(fhStr);System.out.println(hw.getContent());}}依賴注入——案例分析方法二分析優(yōu)點(diǎn):業(yè)務(wù)邏輯(HelloWorld類)和數(shù)據(jù)訪問(HelloStr接口)完全解耦(通過構(gòu)造注入)缺點(diǎn):界面邏輯(HelloWorldClient類)和另外兩層均有依賴。解決方案到系統(tǒng)外部來解決這種依賴關(guān)系——工廠模式。構(gòu)建一個(gè)HelloworldFactory工廠類來處理類之間的耦合關(guān)系。依賴注入——案例分析方法三:HelloWorldFactory類public

classHelloWorldFactory{

public

staticHelloWorldgetFileHelloWorld(){HelloStrhStr=newFileHelloStr("perties");HelloWorldhw=newHelloWorld(hStr);

returnhw;}}方法三:HelloWorldClient類public

classHelloWorldClient{

public

static

voidmain(String[]args){HelloWorldhw=HelloWorldFactory.getFileHelloWorld();System.out.println(hw.getContent());}}依賴注入——案例分析方法三分析優(yōu)點(diǎn):業(yè)務(wù)邏輯(HelloWorld類)、數(shù)據(jù)訪問(HelloStr接口)和界面邏輯(HelloWorldClient類)三者之間完全解耦。HelloWorldFactory類負(fù)責(zé)創(chuàng)建和集成客戶應(yīng)用所需的對(duì)象。借助依賴注入(DI,DependencyInjection)和工廠模式實(shí)現(xiàn)了控制反轉(zhuǎn)(IoC,InversionofControl)。缺點(diǎn):這種控制反轉(zhuǎn)依然需要硬編碼來實(shí)現(xiàn)。解決方案借助框架及外部配置文件來實(shí)現(xiàn)解耦和控制反轉(zhuǎn)。依賴注入——案例分析方法四:配置文件applicationContext.xml<beanname="fileHelloWorld"class="com.openv.spring.HelloWorld"><constructor-arg> <refbean="fileHello"/></constructor-arg></bean><beanname="fileHello"class="com.openv.spring.FileHelloStr"><constructor-arg> <value>perties</value></constructor-arg></bean>方法四:HelloWorldClient類public

classHelloWorldClient{

publicHelloWorldClient(){Resourceresource=newClassPathResource("applicationContext.xml");BeanFactoryfactory=newXmlBeanFactory(resource);HelloWorldhw=(HelloWorld)factory.getBean("fileHelloWorld");System.out.println(hw.getContent());}

public

static

voidmain(String[]args){

newHelloWorldClient();}}依賴注入的方式依賴注入有兩種常見的方式:構(gòu)造注入設(shè)值注入(setter注入)set注入<beanid="userBiz"class="...UserBizImpl"><propertyname="userDAO"ref="userDAO"/></bean>構(gòu)造注入<beanname="fileHelloWorld"class="com.openv.spring.HelloWorld"><constructor-arg> <refbean="fileHello"/></constructor-arg></bean><beanname="fileHello"class="com.openv.spring.FileHelloStr"><constructor-arg> <value>perties</value></constructor-arg></bean>兩種注入方式比較設(shè)值注入的優(yōu)點(diǎn)與傳統(tǒng)JavaBean寫法類似,依賴關(guān)系直觀、自然對(duì)于復(fù)雜依賴關(guān)系,設(shè)值注入能避免構(gòu)造器過于復(fù)雜在依賴關(guān)系(參數(shù))可選時(shí),能避免構(gòu)造器重載構(gòu)造注入的優(yōu)點(diǎn)構(gòu)造注入能表達(dá)依賴關(guān)系的順序構(gòu)造注入方式中不使用setter方法,能避免后續(xù)代碼對(duì)依賴關(guān)系的破壞構(gòu)造注入方式中依賴關(guān)系只能由組件創(chuàng)建者決定,對(duì)組件調(diào)用者而言,依賴關(guān)系完全透明,更符合高內(nèi)聚、低耦合的原則。SpringIoC的核心組件BeanFactory位于org.springframework.beans.factory包中。使開發(fā)者借助于配置文件(如XML或?qū)傩晕募?,能夠?qū)崿F(xiàn)對(duì)JavaBean的配置和管理。主要用于JavaSE應(yīng)用。ApplicationContext位于org.springframework.context包中。ApplicationContext構(gòu)建在BeanFactory基礎(chǔ)之上。除了具有BeanFactory的功能之外,還添加了大量功能,如Spring

IoC集成、國際化資源、事件機(jī)制。主要用JavaEE應(yīng)用。依賴注入使用范例如何開發(fā)一個(gè)打印機(jī)?打印機(jī)功能的實(shí)現(xiàn)依賴于墨盒和紙張。步驟:

1、定義墨盒和紙張的接口標(biāo)準(zhǔn)。

2、使用接口標(biāo)準(zhǔn)開發(fā)打印機(jī)。

3、組裝打印機(jī)。

4、運(yùn)行打印機(jī)。使用依賴注入第一步:定義組件接口墨盒接口:Ink紙張接口:PaperpublicinterfaceInk{publicStringgetColor(intr,intg,intb);}publicinterfacePaper{publicstaticfinalStringnewline="\r\n";/***輸出字符到紙張

*/publicvoidputInChar(charc);/***得到輸出到紙張上的內(nèi)容

*/publicStringgetContent();}使用依賴注入第二步:使用接口開發(fā)打印機(jī)publicclassPrinter{ publicInkink=null; publicPaperpaper=null; publicvoidprint(Stringstr){ System.out.println("使用"+

ink.getColor(255,200,0).+"顏色打印"); for(inti=0;i<str.length();++i){ //逐字符輸出到紙張

paper.putInChar(str.charAt(i)); } System.out.print(paper.getContent());//將紙張的內(nèi)容輸出

}}使用依賴注入第三步:組裝打印機(jī)

a.為了方便組裝,給Printer類的ink和paper屬性添加setter屬性。publicclassPrinter{ publicInkink=null; publicPaperpaper=null; ...... publicvoidsetInk(Inkink){ this.ink=ink;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論