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

下載本文檔

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

文檔簡介

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

:Spring是一個開源框架,于2003年興起的一個輕量級的Java開發(fā)框架。Spring的用途不僅限于服務(wù)器端的開發(fā)。從簡單性、可測試性和松耦合的角度而言,任何Java應(yīng)用都可以從Spring中受益。目標:使現(xiàn)有技術(shù)更加易用,推進編碼最佳實踐。內(nèi)容:依賴注入容器,AOP實現(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》,對企業(yè)級應(yīng)用開發(fā),特別是Java領(lǐng)域。核心理念:Don’tReinventtheWheel.依賴注入的來源復(fù)雜的軟件系統(tǒng)面向?qū)ο笫瓜到y(tǒng)的實現(xiàn)變得簡單當(dāng)系統(tǒng)復(fù)雜到一定程度時,僅僅面向?qū)ο蟛荒芙档蛷?fù)雜性依賴注入的來源組件化核心思想:解耦合,將組件的構(gòu)建和組件的使用分開,實現(xiàn)每個組件塊時只考慮組件內(nèi)部的事情,要點是明確和定義組件間的接口。組件的使用組件的生產(chǎn)接口的定義運行時注入依賴注入——案例分析比如要完成一個小項目,這個項目的功能是在用戶界面展現(xiàn)一個字符串(如Helloworld)。這個項目至少包含3個功能模塊:從持久數(shù)據(jù)(DB)中讀取字符串;對讀取的數(shù)據(jù)進行處理(編碼、格式等);將數(shù)據(jù)展現(xiàn)在用戶界面(命令行、GUI、HTML)。依賴注入——案例分析方法一:最基本的面向?qū)ο蟮膶崿F(xiàn)方式。針對三個功能模塊分別編寫三個類: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)致可維護性和擴展性差,不適合大型、企業(yè)級的應(yīng)用開發(fā)。解決方案用面向接口編程的思想,將業(yè)務(wù)邏輯層(HelloWorld)和DAO層(FileHelloStr)的耦合消除。將HelloWorld類中對具體的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)點:業(yè)務(wù)邏輯(HelloWorld類)和數(shù)據(jù)訪問(HelloStr接口)完全解耦(通過構(gòu)造注入)缺點:界面邏輯(HelloWorldClient類)和另外兩層均有依賴。解決方案到系統(tǒng)外部來解決這種依賴關(guān)系——工廠模式。構(gòu)建一個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)點:業(yè)務(wù)邏輯(HelloWorld類)、數(shù)據(jù)訪問(HelloStr接口)和界面邏輯(HelloWorldClient類)三者之間完全解耦。HelloWorldFactory類負責(zé)創(chuàng)建和集成客戶應(yīng)用所需的對象。借助依賴注入(DI,DependencyInjection)和工廠模式實現(xiàn)了控制反轉(zhuǎn)(IoC,InversionofControl)。缺點:這種控制反轉(zhuǎn)依然需要硬編碼來實現(xiàn)。解決方案借助框架及外部配置文件來實現(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)點與傳統(tǒng)JavaBean寫法類似,依賴關(guān)系直觀、自然對于復(fù)雜依賴關(guān)系,設(shè)值注入能避免構(gòu)造器過于復(fù)雜在依賴關(guān)系(參數(shù))可選時,能避免構(gòu)造器重載構(gòu)造注入的優(yōu)點構(gòu)造注入能表達依賴關(guān)系的順序構(gòu)造注入方式中不使用setter方法,能避免后續(xù)代碼對依賴關(guān)系的破壞構(gòu)造注入方式中依賴關(guān)系只能由組件創(chuàng)建者決定,對組件調(diào)用者而言,依賴關(guān)系完全透明,更符合高內(nèi)聚、低耦合的原則。SpringIoC的核心組件BeanFactory位于org.springframework.beans.factory包中。使開發(fā)者借助于配置文件(如XML或?qū)傩晕募?,能夠?qū)崿F(xiàn)對JavaBean的配置和管理。主要用于JavaSE應(yīng)用。ApplicationContext位于org.springframework.context包中。ApplicationContext構(gòu)建在BeanFactory基礎(chǔ)之上。除了具有BeanFactory的功能之外,還添加了大量功能,如Spring

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

1、定義墨盒和紙張的接口標準。

2、使用接口標準開發(fā)打印機。

3、組裝打印機。

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

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

*/publicStringgetContent();}使用依賴注入第二步:使用接口開發(fā)打印機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)容輸出

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

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

溫馨提示

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

評論

0/150

提交評論