三大框架spring3.0上課提綱_第1頁
三大框架spring3.0上課提綱_第2頁
三大框架spring3.0上課提綱_第3頁
三大框架spring3.0上課提綱_第4頁
三大框架spring3.0上課提綱_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

Spring3.0publicclassStudentActionpublicclassStudentAction privateStudentServicestudentService=privateStudentpublicStudentgetStudent()return}publicvoidsetStudent(Studentstudent)this.student=}publicStringsave(){return"success";}}同樣在業(yè)務(wù)邏輯層調(diào)用數(shù)據(jù)層中,也會出現(xiàn)如下問題publicpublicclassStudentServiceimplimplementsStudentService 層也要切換,但是目前new之后不方便修privateStudentDaostudentDao=newpublicvoidsave(Studentstudent){}}以上代碼是有問題的,即在控制層中依賴了具體的業(yè)務(wù)邏輯層在業(yè)務(wù)邏輯層中依賴了具體的數(shù)據(jù)層.這樣就和具體的實現(xiàn)類耦合在了一起也沒有發(fā)揮面向接口編程的作用.udetDaocleIml需要同時修改,這樣會非常麻煩.class文件,覆蓋原來的文件.不利于項目的后期,而且項目與項目之間的依賴隱藏在了代碼中.也不適合全局把握正個項目,因此性和二次開發(fā)比較麻煩可以使用抽象工廠,StudentDaoStudentService的實現(xiàn)在在工廠中生成,而工廠可以根據(jù)配置文件的指定類型,來創(chuàng)建不同的對象,而且工廠本身一般是不變的.從而降低了對可以變的業(yè)務(wù)邏輯類的依賴,接近的軟件的設(shè)計目標”高內(nèi)聚,低耦合”publicclassStudentActionpublicclassStudentAction依賴具體的對象,改為依賴工廠而工廠創(chuàng)建的路徑來自具體的配置文件,所以靈活必須 privateStudentServiceprivateStudentpublicStudentgetStudent()return}publicvoidsetStudent(Studentstudent)this.student=}publicStringsave(){return"success";}}業(yè)務(wù)邏輯層→抽象工廠→數(shù)據(jù)publicpublicclassStudentServiceImplimplementsStudentServiceprivateStudentDaopublicvoidsave(Studentstudent){}}個實現(xiàn)類,只需要修改配置文件就可以了,注意這里只是降低了層與層之間的復(fù)雜度,并沒有讓耦合,實際上層與層是不可能沒有耦合的,我們學框架的目的就是要找到變的部分,OK了,不需要修改代碼本身建的時候有些類需要初始化操作,有些又不需要初始化數(shù)據(jù)…..這又怎么辦呢?那就要再完善一下這個工廠類了,或者多創(chuàng)建一些工廠了.讓工廠可以處理這些問題,但是我們自己實2:IOC法注入到組件中,我們只需要用即可,而不需要在去管理對象的創(chuàng)建和銷毀我們期望的代publicpublicclassStudentAction//privateStudentServicepublicvoidsetStudentService(StudentServicestudentService)this.studentService=}privateStudentpublicStudentgetStudent()return}publicvoidsetStudent(Studentstudent)this.student=}publicStringsave(){return"success";}}publicpublicclassStudentServiceImplimplementsStudentService//privateStudentDaopublicvoidsave(Studentstudent){}}問題描述:publicpublicvoidsave(StudentSession//手動控制的事務(wù)稱為編 try}}catch(HibernateExceptione){}}以上代碼是有問題的,紅色的部分,每次只要有數(shù)據(jù)修改操作的時候則都要編寫,要的是這些代碼與方法的功能本身又沒有緊密的聯(lián)系.而且基本每個模塊都有,重復(fù)的代碼大大降低了我們的效率和增加了的成本,解決方案1:模publicclass implementsSessionprivateSessionsession=null;publicSession (Sessionsession){this.session=}publicvoidbegin(){}publicvoidrollback(){}publicvoidsave(Studentstudent)try}catch(Exceptione)}finally}}publicvoidcommit(){}}:publicvoidsave(StudentHibernateSessionFactory返回的是session的 的savebegincommit或者} .而且 但是我們的事務(wù)基本所有的業(yè)務(wù)邏輯模塊都有.}解決方案2:AOP式事Spring3.0Spring的IOC容器包含并管理應(yīng)用對象的配置和生命周期,你可以配置你的每個SpringAOP提供了很多基礎(chǔ)但是與業(yè)務(wù)邏輯無關(guān)的功能,比如:事務(wù)管理、與持久Spring還提供了自己的MVC實現(xiàn),和 SpringSpring容器提供了AOP技術(shù),利用它很容易實現(xiàn)如權(quán)限、運行期等功能容器提供的眾多輔作類,使用這些類能夠加快應(yīng)用的開發(fā),如:JdbcTem SpringSpringSpring框架地址: oSpringJarSpirng3-AOPAOP功能,Spring3-CoreIOC功能,Spring3-Persistence-JDBC:SpringJDBCSpring配置文件時,方法1:讓機器上網(wǎng),eclipse會自動從網(wǎng)絡(luò)上schema文件并緩存在硬盤上2:schema文件,方法如下:1:windows→preferences→myeclipse→filesandeditors→xml2:點"add",在出現(xiàn)的窗口中的KeyType中選擇URI,在location中選"Filesystem",spring解壓的dist/resourcesspring-beans-location,Key改為"""applicationContext.xml取代了前面的properties配置文件<bean 指向另一個id此處說明studentService依賴studentDaostudentDao屬性是私有的,必須通過set方法注入<propertyname="studentDao"ref="studentDao"配置studentDaoclass是具體的業(yè)務(wù)邏輯bean<beanid="studentDao" .ssh.b.StudentDaoImpl"publicpublicclass_IOCTestpublicvoidResourceSpringResourceresource=new 建BeanFactorybeanFactory=newgetBean通過name和id去查找實例化的對象,通過Spring容器拿出來的對Studentstudent=newStudent("test",13);}}Spring來管理Action<beanSpring來管理Action<beanid="studentAction"action在創(chuàng)建的時候會把service通過set<propertyname="studentService"ref="studentService"@Test@Test測試Spring管理Action-->Service--publicvoidtest02(){Resourceresource=newBeanFactorybeanFactory=newStudent應(yīng)該通過前臺傳入,Studentstudent=newStudent("test2",13);}}Spring的依賴注入功能,Spring的配置文件,y來獲取想要的n對象,代碼中只聲明了一個接口類型例如、o這樣大大提高了代碼的靈活性(消除了編譯時依賴,讓運行時依賴具體的接口類型,而且以后可以動態(tài)變化g給C功能classpathTomcatapplication配置文件,必@Test測試獲取Resourcepublic@Test測試獲取Resourcepublicvoidtest03()throwsException{Resourceresource=null;BeanFactorybeanFactory=null;//resource=newClassPathResource("applicationContext.xml");beanFactory=newXmlBeanFactory(resource);//通過文 beanFactory=newXmlBeanFactory(resource);}org.springframework.core.io.Resourceresource=null;ApplicationContextBeanFactoryBeanFactoryBean容器,ApplicationContextApplicationContextBeanFactory,如它可以加載外部的資源文件、可以自AOP切面........classpathTomcatapplication配置文件,必@Test測試Applicationpublic@Test測試Applicationpublicvoidtest04()throwsException{ApplicationContextcontext=new}://必須放到web環(huán)境下執(zhí)行下面的語句SpringIOC詳解(重點<beanid="studentDao"<beanid="studentDao" .ssh.c.StudentDaoImpl" factory-method="getBean"publicpublicclassStudentDaoFactorypublicstaticStudentDaogetBean(){ StudentDaoImplimpl=newreturn}}factory-beanref一個普通工廠<beanid="studentDao"factory-bean="studentDaoFactory"method="getBean" .ssh.c.StudentDaoFactory"publicvoid}BeangetBean()時才生成實例)單例模式+publicvoidpublicvoidtest02(){ApplicationContextcontext=new} 單例模式+懶漢式創(chuàng)建(用的時候在創(chuàng)建)注意:<bean<beanid="studentDao"init="true".ssh.c.StudentDaoImpl" bean<beans<beansdefault-lazy-init="true"SingletionSpring容器管理的都是業(yè)務(wù)邏輯類,因為只有業(yè)務(wù)邏輯類才會經(jīng)常變化.業(yè)務(wù)邏輯類盡量避免設(shè)計全局變量,Singletion模式可能會出現(xiàn)問題,90%的時候業(yè)務(wù)邏輯類餓漢模式即可,也就是項目啟動的時候初始化Action必須是多例的,所以要配置prototype屬性,<beanid="studentAction"Action必須是多例的,所以要配置prototype屬性,<beanid="studentAction" <!--studentService是否為多例,決定studentServicescope<propertyname="studentService"ref="studentService"@Test@Test//測試actionscope="prototypepublicvoidtest03(){ApplicationContextcontext=new }Prototypescopeprototype則默認是懶漢模式,因為多例在運行時候創(chuàng)建是沒有意義的.Spring不負責prototype的銷毀.換句話來說,Spring把創(chuàng)建和實體類,Spring中.S2SHAction依賴的實體類還是要通過Struts器賦值,實體類經(jīng)常是通過程序賦值和而非依賴注入賦值publicpublicvoid }publicvoid }@Test//@Test//測試initpublicvoidtest04(){ClassPathXmlApplicationContextcontext=new }中Bean的周期總ClassPathXmlApplicationContextclose()destory方法ApplicationContextclose()方法,需要強轉(zhuǎn)為具體的實現(xiàn)類才可以調(diào)用singleton時才會有效)<bean<bean.ssh.c.StudentServiceImpl"lazy-<propertyname="studentDao"ref="studentDao"<beanid="studentDao" <constructor-arg<constructor-argname="sname"value="admin"<constructor-argname="sid"value="1"<constructor-argname="sage"value="19"<beanid="student"通過參數(shù)編號指定第一個參數(shù)編號為0<beanid="student" <constructor-argindex="1"value="admin"<constructor-argindex="0"value="1"<constructor-argindex="2"value="19"publicpublicclassCollectionTestprivateString[]SetprivateSet<String>ListprivateList<Date>MapprivateMap<String,Student>PropertiesprivatePropertiespublicList<Date>getLists()return}publicvoidsetLists(List<Date>lists)this.lists=}publicMap<String,Student>getMaps()return}publicvoidsetMaps(Map<String,Student>maps)this.maps=}publicString[]getStrs()return}publicvoidsetStrs(String[]strs)this.strs=}publicSet<String>getSets()return}publicvoidsetSets(Set<String>sets)this.sets=}publicPropertiesgetPros()return}publicvoidsetPros(Propertiespros)s=}}<beanid="collectionTest"<beanid="collectionTest" <property<propertydate為日期bean的id<ref<property<property<entrykey="a"value-ref="student"<property<prop<prop<beanid="date"class="java.util.Date" <property<!--配置文件相同的時候后面的會

溫馨提示

  • 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)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論