第六階段ssh三大框架spring_第1頁
第六階段ssh三大框架spring_第2頁
第六階段ssh三大框架spring_第3頁
第六階段ssh三大框架spring_第4頁
第六階段ssh三大框架spring_第5頁
已閱讀5頁,還剩34頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、Spring第一課大綱 Spring框架簡單介紹 Spring IOC配置Bean創(chuàng)建Bean作用范圍依賴注入Spring框架簡單介紹Spring的核心是個(gè)輕量級(jí)(Lightweight)的容器(Container),它是實(shí)現(xiàn)IOC(Inversion of Control)容器和非侵入性(No intrusive)的框架,并提供AOP(Aspect-oriented programming)概念的實(shí)現(xiàn)方式;提供對(duì)持久層(Persistence)、事務(wù)(Transaction)的支持;提供對(duì)MVC Web框架的實(shí)現(xiàn),并對(duì)于一些常用的企業(yè)服務(wù)API(Application Interface)提

2、供一致的模型封裝,是一個(gè)全方位的應(yīng)用程序框架(Application framwork),除此之外,對(duì)于現(xiàn)存的各種框架(Struts, JSF, Hibernate等),Spring也提供了與它們相整合的方案。Spring是一個(gè)輕量級(jí)的j2ee的框架,核心是功能強(qiáng)大的容器IoC,在容器的基礎(chǔ)上又提供對(duì)AOP的支持,以及對(duì)持久層和表示層的數(shù)據(jù)封裝,同時(shí)也對(duì)持久層和表示層組件的支持.Spring框架簡單介紹 區(qū)別Struts和Hibernate Struts是實(shí)現(xiàn)MVC模式的WEB組件,是一個(gè)比較成熟的被廣泛應(yīng)用在WEB應(yīng)用系統(tǒng)的View層。 Hibernate是成熟的ORM組件,主要應(yīng)用實(shí)現(xiàn)數(shù)據(jù)

3、持久層的功能,而最大的作用是面向?qū)ο蟮膉ava語言與關(guān)系數(shù)據(jù)庫之間的橋梁,是我們的開發(fā)人員可以直接通過對(duì)java對(duì)象的持久化操作來完成對(duì)數(shù)據(jù)持久化的操作。Spring框架簡單介紹相關(guān)術(shù)語輕量級(jí)(Lightweight)輕量級(jí)是相對(duì)于一些重量級(jí)的容器(如EJB容器)來說的,Spring的核心包在文件容量上只有不到1MB的大小,使用Spring核心包所需要的資源也是很少的,您甚至可以在小型設(shè)備中使用Spring。(現(xiàn)在輕量級(jí)和重量級(jí)的確很難區(qū)分了,標(biāo)準(zhǔn)越來越多,也越來越難以衡量。EJB有輕量化的傾向,Spring也有重量化的趨勢(shì)。Spring core算是輕量級(jí),但是加上其余的部件,估計(jì)也得列入重

4、量級(jí)的范疇了。)相關(guān)術(shù)語非侵入性(No intrusive)框架原來的用意是提供一個(gè)架構(gòu)的實(shí)現(xiàn),讓開發(fā)人員可以在基本框架的基礎(chǔ)上,快速地開發(fā)出遵循架構(gòu)所需的應(yīng)用程序,然而有些框架一旦被使用,應(yīng)用程序就對(duì)框架有了依賴性,例如使用了大量框架的API,或者直接繼承API的某些類型等(如struts 1.x)。相關(guān)術(shù)語Inversion of Control與Dependency Injection “控制反轉(zhuǎn)” IOC “依賴注入” DIAOP(Aspect-oriented programming) 下次課講解控制反轉(zhuǎn)(IoC)依賴注入(Dependency Injection)服務(wù)定位(Serv

5、ice Locator)面向切/方面的編程(AOP)持久層的封裝和事務(wù)管理Spring主要功能 IOC的control是控制的意思,其實(shí)背后的意義也是一種依賴關(guān)系的轉(zhuǎn)移。如果A依賴于B,其意義其B擁有控制權(quán)。如果轉(zhuǎn)移這種關(guān)系(依賴關(guān)系的反轉(zhuǎn)即控制關(guān)系的反轉(zhuǎn)),將控制權(quán)由實(shí)現(xiàn)的一方轉(zhuǎn)移至抽象的一方,由抽象方擁有控制權(quán),可以獲得組件的可重用性。控制反轉(zhuǎn)(IOC)IoC容器工作原理示意圖 The spring containerFull configured systemReady for useMagic happens hereYour Business Objects (POJOS)Confi

6、guration Metadataproduces控制反轉(zhuǎn)(IOC)配置原數(shù)據(jù) 如何配置Bean以及Bean 之間的關(guān)系,如何進(jìn)行Bean的實(shí)例化及Bean的實(shí)例對(duì)象之間關(guān)系的管理。 有三種方式:用xml格式(通用的配置方式)屬性文件通過Spring所提供的API來通過編程的方式進(jìn)行Bean的初始化工作實(shí)現(xiàn)IOC 實(shí)現(xiàn)IOC容器的初始化ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext.xml);實(shí)現(xiàn)IOCIoC 容器的使用 一個(gè)簡單的例子,來了解IoC容器的基本用法:開發(fā)業(yè)務(wù)處理

7、對(duì)象:public class IoCService private String message;/基本屬性public String getMessage() return message;/提供set方法,為了在IOC容器中進(jìn)行初始化public void setMessage(String message) this.message = message;public void display( ) /基本的業(yè)務(wù)方法System.out.println( getMessage() );實(shí)現(xiàn)IOC實(shí)現(xiàn)IOC配置Bean: Hello! IoC container!/測(cè)試主程序的實(shí)現(xiàn) pac

8、kage cn.spring.ioc; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class IoCMain()public static void main(String args ) ApplicationContext context = new ClassPathXmlApplic

9、ationContext(applicationContext.xml); IoCService service= (IoCService) context.getBean(“iocService”);service.display();執(zhí)行結(jié)果是輸出字符串: “Hello! IoC container!”實(shí)現(xiàn)IOC配置文件的合并Spring的IoC容器除了可以管理業(yè)務(wù)對(duì)象外,還可以管理訪問數(shù)據(jù)庫時(shí)使用的DAO,表示層使用的Action等由于任何Java對(duì)象都可以通過Spring的IoC容器來進(jìn)行管理,所以其配置文件就會(huì)變得非常復(fù)雜和龐大。因此,在實(shí)際的項(xiàng)目中,會(huì)按照被配置對(duì)象的特征或項(xiàng)目的功

10、能模塊來將配置信息放置在不同的配置文件中。Spring提供了兩種解決方案,一種是通過編程的方式來逐一加載配置文件,另一種是使用元素從另一個(gè)或多個(gè)文件加載Bean定義,合并方法如下所示:配置文件的合并配置文件的合并方法:.Bean的創(chuàng)建Spring的IoC容器具有Bean對(duì)象實(shí)例化的功能,對(duì)常用的對(duì)象實(shí)例化方法和設(shè)計(jì)模式提供了支持。主要包括以下三種:使用構(gòu)造方法創(chuàng)建Bean實(shí)例使用靜態(tài)工廠方法創(chuàng)建Bean的實(shí)例(略)。使用實(shí)例化的工廠方法創(chuàng)建Bean的實(shí)例(略)。使用構(gòu)造方法創(chuàng)建Bean實(shí)例,配置方法如下:等價(jià)于 new constructBean()來實(shí)現(xiàn)對(duì)象的實(shí)例化Bean的Scope在Be

11、an的配置文件中,不僅可以控制Bean的各種依賴關(guān)系,還可以控制該對(duì)象的作用域,作用域的配置是通過元素的scope屬性來進(jìn)行配置的。Spring框架默認(rèn)支持5種作用域: singleton IoC容器只會(huì)創(chuàng)建該Bean的唯一實(shí)例; prototype IoC容器在每次請(qǐng)求該Bean的時(shí)候都會(huì)創(chuàng)建一個(gè)新的實(shí)例;Bean的Scoperequest 在一次HTTP請(qǐng)求中,IoC容器會(huì)返回該Bean的同一個(gè)實(shí)例,對(duì)于不同的用戶請(qǐng)求,則會(huì)返回不同的實(shí)例;session 在一個(gè)HTTP session 中,IoC容器會(huì)返回該Bean的同一個(gè)實(shí)例,對(duì)于不同的用戶session,則會(huì)返回不同的實(shí)例;globa

12、l session 在一個(gè)全局的HTTP session中,IoC容器會(huì)返回該Bean的同一個(gè)實(shí)例。Bean的scopeNote:Bean的作用域中request,session和global session只能在Web應(yīng)用環(huán)境中使用ApplicationContext的時(shí)候使用。對(duì)于使用singlton作用域的Bean,通常都是無狀態(tài)的。同一個(gè) Java對(duì)象可以使用元素進(jìn)行多次定義,IoC容器會(huì)獨(dú)立地處理每一個(gè)元素所進(jìn)行的定義。依賴注入是Spring IoC容器實(shí)現(xiàn)反轉(zhuǎn)控制的方式, Spring 的IoC容器以依賴注入的方式實(shí)現(xiàn)了Bean對(duì)象之間關(guān)系的維護(hù)。兩種方式的對(duì)象注入:1、基于構(gòu)造方

13、法的依賴注入 先看一個(gè)簡單的例子: 實(shí)現(xiàn)Bean對(duì)象(見下頁) Construct Injectionpackage com.injection.bean;public class ConstructInjectionBean private AnotherBean anotherBean; private YetAnotherBean yetAnotherBean; private int I; public ConstructInjectionBean( AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) this.

14、anotherBean = anotherBean;this.yetAnotherBean = yetAnotherBean; this.i = i; public void display() System.out.println( ConstructInjectionBean: );System.out.println( this.yetAnotherBean );System.out.println( i ); Construct InjectionConstruct Injection 配置Bean Construct Injection測(cè)試主程序public class Inject

15、ionMain public static void main(String args) ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext.xml); ConstructInjectionBean constructInjectionBean = (ConstructInjectionBean)context.getBean( constructInjectionBean );constructInjectionBean.display();Construct Injection

16、上面的配置類似于下面的java代碼: AnotherBean anotherBean = new AnotherBean(); YetAnotherBean yetAnotherBean = new YetAnotherBean(); ConstructInjectionBean constructInjectionBean = new ConstructInjectionBean (anotherBean, yetAnotherBean,1);注: 在使用構(gòu)造方法進(jìn)行Bean的初始化時(shí),Bean類中構(gòu)造參數(shù)與XML配置文件中的配置元素應(yīng)該是一一對(duì)應(yīng)的;Construct InjectionS

17、etter Injection2、基于setter方法的依賴注入 使用setter方法的依賴注入,需要首先通過調(diào)用無參構(gòu)造器或無參靜態(tài)工廠方法實(shí)例化Bean,然后再調(diào)用該Bean的setter方法實(shí)現(xiàn)setter依賴注入; Demo:Setter Injectionpublic class SetterInjectionBean private AnotherBean anotherBean;private YetAnotherBean yetAnotherBean;private int i;public AnotherBean getAnotherBean() return another

18、Bean;public void setAnotherBean(AnotherBean anotherBean) this.anotherBean = anotherBean;public int getI() return i;public void setI(int i) this.i = i;public YetAnotherBeangetYetAnotherBean() return yetAnotherBean;public void setYetAnotherBean(YetAnotherBean yetAnotherBean) this.yetAnotherBean = yetA

19、notherBean;public void display() System.out.println( SetterInjectionBean: );System.out.println( this.anotherBean );System.out.println( this.yetAnotherBean );System.out.println( i ); !- Setter Injection/測(cè)試主程序public class InjectionMain public static void main(String args) ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext.xml); /* * 基于setter方法的依賴注入 */SetterInjectionBean setterInjectionBean = (SetterInjectionBean)context.getBean( setterInjectionBean );setterInjectionBean.display();Setter Injection自動(dòng)綁定除了在Bean定義文件中使用指定字符串以及基本類型值,使用直接指定參考至其它Bean實(shí)例,或是使用標(biāo)簽指定“cl

溫馨提示

  • 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)論