Tải bản đầy đủ - 0 (trang)
Chapter 6. Overview of the Spring Framework

Chapter 6. Overview of the Spring Framework

Tải bản đầy đủ - 0trang

Mymotivationforwritingthisarticlecamefromthemany

projectsIranacrossthatwereusingEJBsinthewrongmanner.

Forexample,Isawnondistributeduseofthisdistributed

technology.IevensawsimplewebapplicationsuseEJBswhen

theycouldhaveeasilyusedplainoldJavaobjects(POJOs)ina

well-designedapplicationwithaclearseparationofthetiers

(presentationandbusinesstiers,forexample).

Furthermore,theroleIplayedinmycompanyrequiredmeto

interviewmanydevelopers,andthatenabledmetogaugewhat

themainstreamcompanies,projects,anddeveloperswere

using.Overathree-to-four-yearperiod,italsoenabledmeto

listentopersonalopinionsofprobablyahundredorso

developersaboutEJBs(andotherJEEcomponents).The

commonthemeIsawwasthatalargemajoritywereusingonly

statelesssessionbeansandtheremainderwereusingamixof

entitybeansandmessage-drivenbeans;Ican'trecallanyone

usingstatefulsessionbeans.

So,whatisthepointofthisstory?Simple.Inmyopinion,Sun

haddefinedastandardthatwaspossiblymoreapplicabletothe

complexapplicationsandlessapplicabletoapplicationsthatdid

notrequirethemanyfacilitiesprovidedbyJEE.Furthermore,

usingEJBonaprojectmeantneedinganEJBcontainerwhich

cansometimestranslateintoanexpensive,applicationserver,if

yourorganizationchoosestogothecommercialrouteversus

usingopensourceproductssuchasJBossApplicationServer

(jboss.com)orApacheGeronimo(geronimo.apache.org).

WhenIcameacrosstheSpringFrameworksometimeago,I

wasthrilledtoseethatIcouldworkwithPOJOsandstillhave

manyoftheservices(similartoJEE/EJB)availabletome,ifI

chosetousethem.Forexample,Springprovidesrobust

declarativetransactionmanagement.

DedicatingtwofullandsomepartialchapterstotheSpring

Frameworkdoesnotdoitjusticebecausethereissomuchto

cover.However,asyouwillsoonsee,theSpringFramework



doesnottakeanall-or-nothingapproach;thatis,youcanpick

andchoosewhichmodulesareapplicabletoyou.Inthisbook,

weareusingSpringfortwomainfeatures:itswebframework

andofcourse,theinversionofcontrol(IoC;alsoreferredtoas

dependencyinjection)services.Inaddition,wewilluseSpring

forschedulingjobsandsendingemails.WewillnotuseSpring

AOPdirectly,butindirectlywhenweusetheSpringWebMVC

Framework.



Note

Fromthispoint,IwillrefertotheSpringFrameworksimplyas

Springinmanyplaces.Also,Iwillusethetermsdependency

injectionandIoCinterchangeably(moreonthislaterinthis

chapter).







What'sCoveredinThisChapter

Thefocusofthischapteristogetahigh-levelunderstandingof

theSpringFrameworkandsomerelatedconcepts(forexample,

dependencyinjection).WewilllookattheSpringWebMVC

Frameworkinthenextchapter,andwe'lllookatotherfeatures

suchasjobschedulingandemailinginlaterchapters.Inthis

chapter,wewill

GetaclearunderstandingofwhattheSpringFrameworkis,

itsfundamentalconcepts,howitisorganized,andthemany

benefitsofusingthisframework

LearnaboutthebasicSpringconceptssuchasdependency

injection,beansandbeanfactory,applicationcontext,

propertyeditors,andmore

UnderstandhowtheSpringFrameworkispackaged,from

developmentanddeploymentperspectives



WhatIsSpring?

WhenpeopleaskmewhatSpringdoes,Ihavetostopandthink

abouthowtoansweritinonesentence,whichisnoteasytodo

asyouwillseeshortly.So,letmefirststartbyquotingan

excerptdirectlyoutoftheSpringFrameworkReference

Documentation(springframework.org)becauseitdescribesthe

frameworkwell:

"...Springprovidesalight-weightsolutionforbuilding

enterprise-readyapplications,whilestillsupportingthe

possibilityofusingdeclarativetransactionmanagement,

remoteaccesstoyourlogicusingRMIorwebservices,

mailingfacilitiesandvariousoptionsinpersistingyour

datatoadatabase.SpringprovidesanMVCframework,

transparentwaysofintegratingAOPintoyoursoftware

andawell-structuredexceptionhierarchyincluding

automaticmappingfromproprietaryexception

hierarchies.

Springcouldpotentiallybeaone-stopshopforallyour

enterpriseapplications,however,Springismodular,

allowingyoutousepartsofit,withouthavingtobringin

therest..."

Let'sexplorethe"modular"aspectofSpringabitfurther.Figure

6.1(alsotakendirectlyoutoftheSpringFrameworkReference

Documentation)isprobablythebestwaytogetanimmediate

understandingofthevariousthingsSpringcando,anditshows

preciselywhyitisdifficulttodescribetheentireSpring

Frameworkinashortsentence.



Figure6.1.OverviewoftheSpringFramework

(takendirectlyfromtheSpringFramework



ReferenceDocumentationfoundon

springframework.org).



[Viewfullsizeimage]



Inthisbook,IuseSpringFramework2.0RC1.



SpringPackagingforDevelopment

TheSpringmodules,showninFigure6.1,areessentially

conceptualgroupsoffunctionalityprovidedbytheextensivelist

ofSpring'sJavapackagesandunderlyingclasses.

Figure6.2showsthevarioustop-levelSpringpackages.

(Note:Theshadedpackagesareoneswewilluseforoursample

application,againdemonstratingthatSpringdoesnottakean

all-or-nothingapproach.)



Figure6.2.Spring'stop-levelJavapackages

underorg.springframework.



OverwhelmedbytheSizeoftheSpring

Framework?



TheSpringFrameworkcontainssomethingalongthelinesof

130+Javapackagesand1,200+Javaclasses.However,donot

beoverwhelmedbythisnumber;hereiswhy.

First,youmaywanttouseonlySpring'sIoCfeatures,soyou

couldessentiallyignoremostoftheAPIs.Second,although

therearelotsofclassesandthere'slotsofJavadoc,manyof

theseclassesareforinternalusebytheframeworkitself.Third,

Springenablesyoutoselectthemodulesyouwanttouseand

ignoretherest.Forexample,wewilluseonlyahandfulofthese

foroursampleapplication,TimeExpressionthisisproofthat

Spring'sdesignishighlymodular.



SpringPackagingforDeployment

ThemodularaspectofSpringextendstodeploymentaswell,

becauseyoucandeployyourapplicationwithonlythesubsetof

SpringJARfilesyouneed(forexample,spring-jdbc.jar).Table

6.1showshowSpringprovidesthepackagesusingvariousJAR

files.Noticehowthespring.jarisbiggerthanothers,becauseit

isthecompletepackage.Itendtoincludethecomplete

spring.jarfileinmyapplicationsbecause1.7MBisn'tconsidered

verylargethesedays,especiallyforserver-sideapplications.

Table6.1.Spring'sCompleteorSeparateJARFiles

JARFile



Size(KB)



spring.jar



1,731



modules/spring-beans.jar



258



modules/spring-aop.jar



224



modules/spring-jdbc.jar



210



modules/spring-webmvc.jar



208



modules/spring-support.jar



189



modules/spring-remoting.jar



166



modules/spring-web.jar



138



modules/spring-core.jar



129



modules/spring-context.jar



111



modules/spring-dao.jar



103



extmodules/spring-hibernate3.jar



102



extmodules/spring-portlet.jar



97



extmodules/spring-hibernate2.jar



85



extmodules/spring-mock.jar



69



extmodules/spring-jdo.jar



61



extmodules/spring-toplink.jar



55



extmodules/spring-ojb.jar



27



aspects/spring-aspects.jar



10



OverviewoftheSpringModules

Nowthatwehaveseenaveryhigh-levelpreviewofSpring,

let'slookatabriefdescriptionofeachmodule.First,wewill

beginwiththetwomostimportantmodulesandthenlookat

theremainingonesinanalphabeticalorder.Again,wewilluse

onlyasubsetofthemodulesdescribednext,sodonotget

overwhelmedbyhowextensivethisframeworkis.



SpringCore

ThecoremoduleisessentiallythefoundationforSpring.It

providesfundamentalfeaturessuchasdependencyinjection

(whichwewilllookatlaterinthechapter)andmanagementof

beans.Someofthetop-levelpackagesthatfitunderthis

moduleareorg.springframework.beans,org.springframework.core,and

org.springframework.util.



SpringContext

Thecontextmoduleisperhapsthesecondmostimportant

moduleinSpring,aftertheSpringCoremodule.Itcontainskey

classessuchasApplicationContext(explainedlaterinthischapter)

andWebApplicationContext,whichwewilluseinTimeExpression

(loadedfromourtimex-servlet.xmlfile,whichisexplainedinthe

nextchapter).Inaddition,wewillusetheorg.springframework.mail

(forsendingemails)andorg.springframework.validation(for

validatingwebUIfields)packages,whicharealsoconsidered

partofthismodule.

Otherpackages,primarilyusedforremote/distributed

functionality(EJBandJNDI,forexample),areoutsidethescope



ofthisbook.



SpringAOP

WewillnotwriteAOPcodedirectlyusingSpring.However,

indirectlywewillendupusingSpringAOP-basedfacilities,such

asinterceptorsforourwebapplicationanddeclarative

transactionmanagement.Infact,theSpringreference

documentationmentionsthesamepoint;thatis,ifthe

prepackagedfunctionalityprovidedinSpringissufficientfor

yourneeds,youdonotneedtouseSpringAOP(towrite

customaspects,forexample).



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 6. Overview of the Spring Framework

Tải bản đầy đủ ngay(0 tr)

×