畢業(yè)設(shè)計外文文獻-Spring_第1頁
畢業(yè)設(shè)計外文文獻-Spring_第2頁
畢業(yè)設(shè)計外文文獻-Spring_第3頁
畢業(yè)設(shè)計外文文獻-Spring_第4頁
畢業(yè)設(shè)計外文文獻-Spring_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

附錄A外文翻譯—原文部分SpringBootinAction.CraigWall.ManningPublications,2016.1.1SpringrebootedSpringstartedasalightweightalternativetoJavaEnterpriseEdition(JEE,orJ2EEasitwasknownatthetime).RatherthandevelopcomponentsasheavyweightEnterpriseJavaBeans(EJBs),SpringofferedasimplerapproachtoenterpriseJavadevelopment,utilizingdependencyinjectionandaspect-orientedprogrammingtoachievethecapabilitiesofEJBwithplainoldJavaobjects(POJOs).ButwhileSpringwaslightweightintermsofcomponentcode,itwasheavyweightintermsofconfiguration.Initially,SpringwasconfiguredwithXML(andlotsofit).Spring2.5introducedannotation-basedcomponent-scanning,whicheliminatedagreatdealofexplicitXMLconfigurationforanapplication’sowncomponents.AndSpring3.0introducedaJava-basedconfigurationasatype-safeandrefactorableoptiontoXML.Evenso,therewasnoescapefromconfiguration.EnablingcertainSpringfeaturessuchastransactionmanagementandSpringMVCrequiredexplicitconfiguration,eitherinXMLorJava.Enablingthird-partylibraryfeaturessuchasThymeleaf-basedwebviewsrequiredexplicitconfiguration.Configuringservletsandfilters(suchasSpring’sDispatcherServlet)requiredexplicitconfigurationinweb.xmlorinaservletinitializer.Component-scanningreducedconfigurationandJavaconfigurationmadeitlessawkward,butSpringstillrequiredalotofconfiguration.Allofthatconfigurationrepresentsdevelopmentfriction.Anytimespentwritingconfigurationistimespentnotwritingapplicationlogic.ThementalshiftrequiredtothinkaboutconfiguringaSpringfeaturedistractsfromsolvingthebusinessproblem.Likeanyframework,Springdoesalotforyou,butitdemandsthatyoudoalotforitinreturn.Moreover,projectdependencymanagementisathanklesstask.Decidingwhatlibrariesneedtobepartoftheprojectbuildistrickyenough.Butit’sevenmorechallengingtoknowwhichversionsofthoselibrarieswillplaywellwithothers.Asimportantasitis,dependencymanagementisanotherformoffriction.Whenyou’readdingdependenciestoyourbuild,you’renotwritingapplicationcode.Anyincompatibilitiesthatcomefromselectingthewrongversionsofthosedependenciescanbearealproductivitykiller.SpringBoothaschangedallofthat.1.1.1TakingafreshlookatSpringSupposeyou’regiventhetaskofdevelopingaverysimpleHelloWorldwebapplicationwithSpring.Whatwouldyouneedtodo?Icanthinkofahandfulofthingsyou’dneedatabareminimum:Aprojectstructure,completewithaMavenorGradlebuildfileincludingrequireddependencies.Attheveryleast,you’llneedSpringMVCandtheServletAPIexpressedasdependencies.Aweb.xmlfile(oraWebApplicationInitializerimplementation)thatdeclaresSpring’sDispatcherServlet.ASpringconfigurationthatenablesSpringMVC.AcontrollerclassthatwillrespondtoHTTPrequestswith“HelloWorld”.Awebapplicationserver,suchasTomcat,todeploytheapplicationto.What’smoststrikingaboutthislististhatonlyoneitemisspecifictodevelopingtheHelloWorldfunctionality:thecontroller.Therestofitisgenericboilerplatethatyou’dneedforanywebapplicationdevelopedwithSpring.ButifallSpringwebapplicationsneedit,whyshouldyouhavetoprovideit?Supposeforamomentthatthecontrollerisallyouneed.Asitturnsout,theGroovy-basedcontrollerclassshowninlisting1.1isacomplete(evenifsimple)Springapplication.@RestControllerclassHelloController{@RequestMapping("/")defhello(){return"HelloWorld"}}There’snoconfiguration.Noweb.xml.Nobuildspecification.Notevenanapplicationserver.Thisistheentireapplication.SpringBootwillhandlethelogisticsofexecutingtheapplication.Youonlyneedtobringtheapplicationcode.AssumingthatyouhaveSpringBoot’scommand-lineinterface(CLI)installed,youcanrunHelloControlleratthecommandlinelikethis:$springrunHelloController.groovyYoumayhavealsonoticedthatitwasn’tevennecessarytocompilethecode.TheSpringBootCLIwasabletorunitfromitsuncompiledform.IchosetowritethisexamplecontrollerinGroovybecausethesimplicityoftheGroovylanguagepresentswellalongsidethesimplicityofSpringBoot.ButSpringBootdoesn’trequirethatyouuseGroovy.Infact,muchofthecodewe’llwriteinthisbookwillbeinJava.Butthere’llbesomeGroovyhereandthere,whereappropriate.Feelfreetolookaheadtosection1.21toseehowtoinstalltheSpringBootCLI,sothatyoucantryoutthislittlewebapplication.Butfornow,we’lllookatthekeypiecesofSpringBoottoseehowitchangesSpringapplicationdevelopment.1.1.2ExaminingSpringBootessentialsSpringBootbringsagreatdealofmagictoSpringapplicationdevelopment.Buttherearefourcoretricksthatitperforms:Automaticconfiguration—SpringBootcanautomaticallyprovideconfigurationforapplicationfunctionalitycommontomanySpringapplications.Starterdependencies—YoutellSpringBootwhatkindoffunctionalityyouneed,anditwillensurethatthelibrariesneededareaddedtothebuild.Thecommand-lineinterface—ThisoptionalfeatureofSpringBootletsyouwritecompleteapplicationswithjustapplicationcode,butnoneedforatraditionalprojectbuild.TheActuator—Givesyouinsightintowhat’sgoingoninsideofarunningSpringBootapplication.EachofthesefeaturesservestosimplifySpringapplicationdevelopmentinitsownway.We’lllookathowtoemploythemtotheirfullestthroughoutthisbook.Butfornow,let’stakeaquicklookatwhateachoffers.AUTO-CONFIGURATIONInanygivenSpringapplication’ssourcecode,you’llfindeitherJavaconfigurationorXMLconfiguration(orboth)thatenablescertainsupportingfeaturesandfunctionalityfortheapplication.Forexample,ifyou’veeverwrittenanapplicationthataccessesarelationaldatabasewithJDBC,you’veprobablyconfiguredSpring’sJdbcTemplateasabeanintheSpringapplicationcontext.I’llbettheconfigurationlookedalotlikethis:@BeanpublicJdbcTemplatejdbcTemplate(DataSourcedataSource){returnnewJdbcTemplate(dataSource);}ThisverysimplebeandeclarationcreatesaninstanceofJdbcTemplate,injectingitwithitsonedependency,aDataSource.Ofcourse,thatmeansthatyou’llalsoneedtoconfigureaDataSourcebeansothatthedependencywillbemet.Tocompletethisconfigurationscenario,supposethatyouweretoconfigureanembeddedH2databaseastheDataSourcebean:@BeanpublicDataSourcedataSource(){returnnewEmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScripts('schema.sql','data.sql').build();}Thisbeanconfigurationmethodcreatesanembeddeddatabase,specifyingtwoSQLscriptstoexecuteontheembeddeddatabase.Thebuild()methodreturnsaDataSourcethatreferencestheembeddeddatabase.Neitherofthesetwobeanconfigurationmethodsisterriblycomplexorlengthy.ButtheyrepresentjustafractionoftheconfigurationinatypicalSpringapplication.Moreover,therearecountlessSpringapplicationsthatwillhavetheseexactsamemethods.AnyapplicationthatneedsanembeddeddatabaseandaJdbcTemplatewillneedthosemethods.Inshort,it’sboilerplateconfiguration.Ifit’ssocommon,thenwhyshouldyouhavetowriteit?SpringBootcanautomaticallyconfigurethesecommonconfigurationscenarios.IfSpringBootdetectsthatyouhavetheH2databaselibraryinyourapplication’sclasspath,itwillautomaticallyconfigureanembeddedH2database.IfJdbcTemplateisintheclasspath,thenitwillalsoconfigureaJdbcTemplatebeanforyou.There’snoneedforyoutoworryaboutconfiguringthosebeans.They’llbeconfiguredforyou,readytoinjectintoanyofthebeansyouwrite.There’salotmoretoSpringBootauto-configurationthanembeddeddatabasesandJdbcTemplate.ThereareseveraldozenwaysthatSpringBootcantaketheburdenofconfigurationoffyourhands,includingauto-configurationfortheJavaPersistenceAPI(JPA),Thymeleaftemplates,security,andSpringMVC.We’lldiveintoautoconfigurationstartinginchapter2.STARTERDEPENDENCIESItcanbechallengingtoadddependenciestoaproject’sbuild.Whatlibrarydoyouneed?Whatareitsgroupandartifact?Whichversiondoyouneed?Willthatversionplaywellwithotherdependenciesinthesameproject?SpringBootoffershelpwithprojectdependencymanagementbywayofstarterdependencies.StarterdependenciesarereallyjustspecialMaven(andGradle)dependenciesthattakeadvantageoftransitivedependencyresolutiontoaggregatecommonlyusedlibrariesunderahandfuloffeature-defineddependencies.Forexample,supposethatyou’regoingtobuildaRESTAPIwithSpringMVCthatworkswithJSONresourcerepresentations.Additionally,youwanttoapplydeclarativevalidationpertheJSR-303specificationandservetheapplicationusinganembeddedTomcatserver.Toaccomplishallofthis,you’llneed(atminimum)thefollowingeightdependenciesinyourMavenorGradlebuild:org.springframework:spring-coreorg.springframework:spring-weborg.springframework:spring-webmvccom.fasterxml.jackson.core:jackson-databindorg.hibernate:hibernate-validatororg.apache.tomcat.embed:tomcat-embed-coreorg.apache.tomcat.embed:tomcat-embed-elorg.apache.tomcat.embed:tomcat-embed-logging-juliOntheotherhand,ifyouweretotakeadvantageofSpringBootstarterdependencies,youcouldsimplyaddtheSpringBoot“web”starter(org.springframework.boot:spring-boot-starter-web)asabuilddependency.Thissingledependencywilltransitivelypullinallofthoseotherdependenciessoyoudon’thavetoaskforthemall.Butthere’ssomethingmoresubtleaboutstarterdependenciesthansimplyreducingbuilddependencycount.Noticethatbyaddingthe“web”startertoyourbuild,you’respecifyingatypeoffunctionalitythatyourapplicationneeds.Yourappisawebapplication,soyouaddthe“web”starter.Likewise,ifyourapplicationwilluseJPApersistence,thenyoucanaddthe“jpa”starter.Ifitneedssecurity,youcanaddthe“security”starter.Inshort,younolongerneedtothinkaboutwhatlibrariesyou’llneedtosupportcertainfunctionality;yousimplyaskforthatfunctionalitybywayofthepertinentstarterdependency.AlsonotethatSpringBoot’sstarterdependenciesfreeyoufromworryingaboutwhichversionsoftheselibrariesyouneed.Theversionsofthelibrariesthatthestarterspullinhavebeentestedtogethersothatyoucanbeconfidentthattherewillbenoincompatibilitiesbetweenthem.Alongwithauto-configuration,we’llbeginusingstarterdependenciesrightaway,startinginchapter2.THECOMMAND-LINEINTERFACE(CLI)Inadditiontoauto-configurationandstarterdependencies,SpringBootalsooffersanintriguingnewwaytoquicklywriteSpringapplications.Asyousawearlierinsection1.1,theSpringBootCLImakesitpossibletowriteapplicationsbydoingmorethanwritingtheapplicationcode.SpringBoot’sCLIleveragesstarterdependenciesandauto-configurationtoletyoufocusonwritingcode.Notonlythat,didyounoticethattherearenoimportlinesinlisting1.1?HowdidtheCLIknowwhatpackagesRequestMappingandRestControllercomefrom?Forthatmatter,howdidthoseclassesendupintheclasspath?TheshortansweristhattheCLIdetectedthatthosetypesarebeingused,anditknowswhichstarterdependenciestoaddtotheclasspathtomakeitwork.Oncethosedependenciesareintheclasspath,aseriesofauto-configurationkicksinandensuresthatDispatcherServletandSpringMVCareenabledsothatthecontrollercanrespondtoHTTPrequests.SpringBoot’sCLIisanoptionalpieceofSpringBoot’spower.AlthoughitprovidestremendouspowerandsimplicityforSpringdevelopment,italsointroducesaratherunconventionaldevelopmentmodel.Ifthisdevelopmentmodelistooextremeforyourtaste,thennoproblem.YoucanstilltakeadvantageofeverythingelsethatSpringBoothastoofferevenifyoudon’tusetheCLI.ButifyoulikewhattheCLIprovides,you’lldefinitelywanttolookatchapter5wherewe’lldigdeeperintoSpringBoot’sCLI.THEACTUATORThefinalpieceoftheSpringBootpuzzleistheActuator.WheretheotherpartsofSpringBootsimplifySpringdevelopment,theActuatorinsteadofferstheabilitytoinspecttheinternalsofyourapplicationatruntime.WiththeActuatorinstalled,youcaninspecttheinnerworkingsofyourapplication,includingdetailssuchasWhatbeanshavebeenconfiguredintheSpringapplicationcontextWhatdecisionsweremadebySpringBoot’sauto-configurationWhatenvironmentvariables,systemproperties,configurationproperties,andcommand-lineargumentsareavailabletoyourapplicationThecurrentstateofthethreadsinandsupportingyourapplicationAtraceofrecentHTTPrequestshandledbyyourapplicationVariousmetricspertainingtomemoryusage,garbagecollection,webrequests,anddatasourceusageTheActuatorexposesthisinformationintwoways:viawebendpointsorviaashellinterface.Inthelattercase,youcanactuallyopenasecureshell(SSH)intoyourapplicationandissuecommandstoinspectyourapplicationasitruns.We’llexploretheActuator’scapabilitiesindetailwhenwegettochapter7.

附錄B外文翻譯—譯文部分1.1Spring風(fēng)云再起Spring誕生時是Java企業(yè)版(JavaEnterpriseEdition,JEE,也稱J2EE)的輕量級代替品。無需開發(fā)重量級的EnterpriseJavaBean(EJB),Spring為企業(yè)級Java開發(fā)提供了一種相對簡單的方法,通過依賴注入和面向切面編程,用簡單的Java對象(PlainOldJavaObject,POJO)實現(xiàn)了EJB的功能。雖然Spring的組件代碼是輕量級的,但它的配置卻是重量級的。一開始,Spring用XML配置,而且是很多XML配置。Spring2.5引入了基于注解的組件掃描,這消除了大量針對應(yīng)用程序自身組件的顯式XML配置。Spring3.0引入了基于Java的配置,這是一種類型安全的可重構(gòu)配置方式,可以代替XML。盡管如此,我們依舊沒能逃脫配置的魔爪。開啟某些Spring特性時,比如事務(wù)管理和SpringMVC,還是需要用XML或Java進行顯式配置。啟用第三方庫時也需要顯式配置,比如基于Thymeleaf的Web視圖。配置Servlet和過濾器(比如Spring的DispatcherServlet)同樣需要在web.xml或Servlet初始化代碼里進行顯式配置。組件掃描減少了配置量,Java配置讓它看上去簡潔不少,但Spring還是需要不少配置。所有這些配置都代表了開發(fā)時的損耗。因為在思考Spring特性配置和解決業(yè)務(wù)問題之間需要進行思維切換,所以寫配置擠占了寫應(yīng)用程序邏輯的時間。和所有框架一樣,Spring實用,但與此同時它要求的回報也不少。除此之外,項目的依賴管理也是件吃力不討好的事情。決定項目里要用哪些庫就已經(jīng)夠讓人頭痛的了,你還要知道這些庫的哪個版本和其他庫不會有沖突,這難題實在太棘手。并且,依賴管理也是一種損耗,添加依賴不是寫應(yīng)用程序代碼。一旦選錯了依賴的版本,隨之而來的不兼容問題毫無疑問會是生產(chǎn)力殺手。SpringBoot讓這一切成為了過去。1.1.1重新認識Spring假設(shè)你受命用Spring開發(fā)一個簡單的HelloWorldWeb應(yīng)用程序。你該做什么?我能想到一些基本的需要。一個項目結(jié)構(gòu),其中有一個包含必要依賴的Maven或者Gradle構(gòu)建文件,最起碼要有SpringMVC和ServletAPI這些依賴。一個web.xml文件(或者一個WebApplicationInitializer實現(xiàn)),其中聲明了Spring的DispatcherServlet。一個啟用了SpringMVC的Spring配置。一個控制器類,以“HelloWorld”響應(yīng)HTTP請求。一個用于部署應(yīng)用程序的Web應(yīng)用服務(wù)器,比如Tomcat。最讓人難以接受的是,這份清單里只有一個東西是和HelloWorld功能相關(guān)的,即控制器,剩下的都是Spring開發(fā)的Web應(yīng)用程序必需的通用樣板。既然所有SpringWeb應(yīng)用程序都要用到它們,那為什么還要你來提供這些東西呢?假設(shè)這里只需要控制器。代碼清單1-1所示基于Groovy的控制器類就是一個簡單而完整的Spring應(yīng)用程序。代碼清單1-1一個完整的基于Groovy的Spring應(yīng)用程序@RestControllerclassHelloController{@RequestMapping("/")defhello(){return"HelloWorld"}}這里沒有配置,沒有web.xml,沒有構(gòu)建說明,甚至沒有應(yīng)用服務(wù)器,但這就是整個應(yīng)用程序了。SpringBoot會搞定執(zhí)行應(yīng)用程序所需的各種后勤工作,你只要搞定應(yīng)用程序的代碼就好。假設(shè)你已經(jīng)裝好了SpringBoot的命令行界面(CommandLineInterface,CLI),可以像下面這樣在命令行里運行HelloController:$springrunHelloController.groovy想必你已經(jīng)注意到了,這里甚至沒有編譯代碼,SpringBootCLI可以運行未經(jīng)編譯的代碼。之所以選擇用Groovy來寫這個控制器示例,是因為Groovy語言的簡潔與SpringBoot的簡潔有異曲同工之妙。但SpringBoot并不強制要求使用Groovy。實際上,本書中的很多代碼都是用Java寫的,但在恰當(dāng)?shù)臅r候,偶爾也會出現(xiàn)一些Groovy代碼。不要客氣,直接跳到1.2.1節(jié)吧,看看如何安裝SpringBootCLI,這樣你就能試著編寫這個小小的Web應(yīng)用程序了?,F(xiàn)在,你將看到SpringBoot的關(guān)鍵部分,看到它是如何改變Spring應(yīng)用程序的開發(fā)方式的。1.1.2SpringBoot精要SpringBoot將很多魔法帶入了Spring應(yīng)用程序的開發(fā)之中,其中最重要的是以下四個核心。自動配置:針對很多Spring應(yīng)用程序常見的應(yīng)用功能,SpringBoot能自動提供相關(guān)配置。起步依賴:告訴SpringBoot需要什么功能,它就能引入需要的庫。命令行界面:這是SpringBoot的可選特性,借此你只需寫代碼就能完成完整的應(yīng)用程序,無需傳統(tǒng)項目構(gòu)建。Actuator:讓你能夠深入運行中的SpringBoot應(yīng)用程序,一探究竟。每一個特性都在通過自己的方式簡化Spring應(yīng)用程序的開發(fā)。本書會探尋如何將它們發(fā)揮到極致,但就目前而言,先簡單看看它們都提供了哪些功能吧。1.自動配置在任何Spring應(yīng)用程序的源代碼里,你都會找到Java配置或XML配置(抑或兩者皆有),它們?yōu)閼?yīng)用程序開啟了特定的特性和功能。舉個例子,如果你寫過用JDBC訪問關(guān)系型數(shù)據(jù)庫的應(yīng)用程序,那你一定在Spring應(yīng)用程序上下文里配置過JdbcTemplate這個Bean。我打賭那段配置看起來是這樣的:@BeanpublicJdbcTemplatejdbcTemplate(DataSourcedataSource){returnnewJdbcTemplate(dataSource);}這段非常簡單的Bean聲明創(chuàng)建了一個JdbcTemplate的實例,注入了一個DataSource依賴。當(dāng)然,這意味著你還需要配置一個DataSource的Bean,這樣才能滿足依賴。假設(shè)你將配置一個嵌入式H2數(shù)據(jù)庫作為DataSourceBean,完成這個配置場景的代碼大概是這樣的:@BeanpublicDataSourcedataSource(){returnnewEmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScripts('schema.sql','data.sql').build();}這個Bean配置方法創(chuàng)建了一個嵌入式數(shù)據(jù)庫,并指定在該數(shù)據(jù)庫上執(zhí)行兩段SQL腳本。build()方法返回了一個指向該數(shù)據(jù)庫的引用。這兩個Bean配置方法都不復(fù)雜,也不是很長,但它們只是典型Spring應(yīng)用程序配置的一小部分。除此之外,還有無數(shù)Spring應(yīng)用程序有著完全相同的方法。所有需要用到嵌入式數(shù)據(jù)庫和JdbcTemplate的應(yīng)用程序都會用到那些方法。簡而言之,這就是一個樣板配置。既然它如此常見,那為什么還要你去寫呢?SpringBoot會為這些常見配置場景進行自動配置。如果SpringBoot在應(yīng)用程序的Classpath里發(fā)現(xiàn)H2數(shù)據(jù)庫的庫,那么它就自動配置一個嵌入式H2數(shù)據(jù)庫。如果在Classpath里發(fā)現(xiàn)JdbcTemplate,那么它還會為你配置一個JdbcTemplate的Bean。你無需操心那些Bean的配置,SpringBoot會做好準(zhǔn)備,隨時都能將其注入到你的Bean里。SpringBoot的自動配置遠不止嵌入式數(shù)據(jù)庫和JdbcTemplate,它有大把的辦法幫你減輕配置負擔(dān),這些自動配置涉及Java持久化API(JavaPersistenceAPI,JPA)、Thymeleaf模板、安全和SpringMVC。第2章會深入討論自動配置這個話題。2.起步依賴向項目中添加依賴是件富有挑戰(zhàn)的事。你需要什么庫?它的Group和Artifact是什么?你需要哪個版本?哪個版本不會和項目中的其他依賴發(fā)生沖突?SpringBoot通過起步依賴為項目的依賴管理提供幫

溫馨提示

  • 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. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論