Spring Basics 1

Embed Size (px)

Citation preview

  • 8/8/2019 Spring Basics 1

    1/70

    1

    Spring FrameworkBasics

  • 8/8/2019 Spring Basics 1

    2/70

    2

    objectives

    Aft er th e comple t ion o f th is t opic th epar t icipan t s h ould be aware o f th ef ollowing t opics

    Wh a t is spring f rame workDependency injec t ionS h ould be able t o work wi th di ff eren t wring

  • 8/8/2019 Spring Basics 1

    3/70

    3

    T opics

    Wh a t is Spring f ramework?Wh y Spring f ramework?Spring f ramework arc h it ec t ure

    Usage scenarioDependency Injec t ion (DI)

    BeanFac t oryA u t owiringA

    pplicationCon

    tex

    t

  • 8/8/2019 Spring Basics 1

    4/70

    4

    Wh a t is Spring Framework?

  • 8/8/2019 Spring Basics 1

    5/70

    5

    Wh a t is Spring Framework?

    Light- weig ht ye t compre h ensivef ramework f or building Java SE and JavaEE applica t ions

  • 8/8/2019 Spring Basics 1

    6/70

    6

    Wh a t is Spring Framework?

    Th e Spring Framework MissionStatement

    J2 EE s h ould be easier t o useI t' s bes t t o program t o in t er f aces, ra th er

    th an classes. Spring reduces th e complexi t ycos t o f using in t er f aces t o zeroJavaBeans o ff er a grea t way o f con f iguring

    applica t ionsOO design is more impor t an t th an any

    implemen t a t ion t ec h nology, suc h as J 2 EECh ecked excep t ions are overused. A

    f ramework s h ould no t f orce t o ca t ch

  • 8/8/2019 Spring Basics 1

    7/70

    7

    K ey Fea t ures

    JavaBeans - based con f igura t ion managemen t ,applying Inversion - o f- Con t rol principles,speci f ically using th e Dependency Injec t iont ec h nique

    T h is aims t o reduce dependencies o f componen t s onspeci f ic implemen t a t ions o f o th er componen t s.

    A core bean f ac t ory, w h ich is usable globallyGeneric abs t rac t ion layer f or da t abase

    t ransac t ion managemen t

  • 8/8/2019 Spring Basics 1

    8/70

    8

    K ey Fea t ures

    Buil t- in generic s t ra t egies f or J T A and a singleJDBC Da t aSource

    T h is removes th e dependency on a Java EE

    environment f or

    transac

    tion suppor

    t.

    In t egra t ion wi th persis t ence f rameworksHiberna t e, JDO and iB A T IS.

    MVC web applica t ion f ramework, buil t on coreSpring f unc t ionali t y, suppor t ing manyt ec h nologies f or genera t ing views, includingJSP, FreeMarker, Veloci t y, T iles, i T ex t , and POI.

  • 8/8/2019 Spring Basics 1

    9/70

    9

    K ey Fea t ures

    Ex t ensive aspec t- orien t ed programming( A OP) f ramework t o provide servicessuc h as t ransac t ion managemen t

    A s wi th th e Inversion - o f- Con t rol par t s o f th e sys t em , th is aims t o improve th emodulari t y o f sys t ems crea t ed using th ef ramework.

  • 8/8/2019 Spring Basics 1

    10/70

    10

    Wh y UseSpring Framework?

  • 8/8/2019 Spring Basics 1

    11/70

    11

    Wh y Use Spring?

    Wiring o f componen t s th roug h Dependency Injec t ion

    Promo t es de - coupling among th e par t s th a t

    make th e applica t ionDesign t o in t er f aces

    Insula t es a user o f a f unc t ionali t y f romimplemen t a t ion de t ails

    T es t- Driven Developmen t ( T DD)POJO classes can be t es t ed wi th ou t being

    t ied up wi th th e f ramework

  • 8/8/2019 Spring Basics 1

    12/70

    1 2

    Wh y Use Spring?

    Declara t ive programming th roug h A OPEasily con f igured aspec t s, esp. t ransac t ion

    suppor t

    Simpli f y use o f popular t ec h nologiesA bs t rac t ions insula t e applica t ion f rom

    speci f ics, elimina t e redundan t codeHandle common error condi t ions

    Underlying t ec h nology speci f ics s t illaccessible

  • 8/8/2019 Spring Basics 1

    13/70

    1 3

    Wh y Use Spring?

    Conversion o f ch ecked excep t ions t ounc h ecked

    (Or is th is a reason no t t o use i t ?)

    No t an all - or - no th ing solu t ionEx t remely modular and f lexible

    Well designedEasy t o ex t endMany reusable classes

  • 8/8/2019 Spring Basics 1

    14/70

    1 4

    Wh y Use Spring?

    In t egra t ion wi th o th er t ec h nologiesEJB f or J 2 EEHiberna t e, iBa t es, JDBC ( f or da t a access)

    Veloci t y ( f or presen t a t ion)S t ru t s and WebWork (For web)

  • 8/8/2019 Spring Basics 1

    15/70

    1 5

    Spring so ft ware

    You can download th e Spring dis t ribu t ionf rom Springs websi t e:htt p://www.spring f ramework.org.

    Ch oose th e downloads link f rom th e le ft-h and menu and look f or th e Spring 2 . 5 download.Download spring -f ramework -2 . 5 - wi th-dependencies.jar f ile

  • 8/8/2019 Spring Basics 1

    16/70

    1 6

    Some o f th e spring jarsspring.jar Con t ains mos t o f th e modules o f th e Spring Framework

    in one convenien t JAR f ile.spring - aspec t s.jar Springs A spec t J- speci f ic classes.

    spring - aop.jar T h e Spring A OP module.

    spring - beans.jarT h e Spring bean f ac t ory module.

    spring - core.jar Classes th a t are core t o th e Spring Framework.

    spring - dao.jar T h e basis f or Springs D A O suppor t .

    spring -h iberna t e 3 .jar Hiberna t e 3 suppor t

    spring - jdbc.jar Spring jdbc module

    spring - webmvc.jar Springs web applica t ion con t ex t and u t ilit ies.

    spring - web.jar Spring mvc module

  • 8/8/2019 Spring Basics 1

    17/70

    1 7

    Spring FrameworkA rc h it ec t ure

  • 8/8/2019 Spring Basics 1

    18/70

    1 8

    Spring Framework

  • 8/8/2019 Spring Basics 1

    19/70

    1 9

    Core Package

    Core package is th e mos t f undamen t al par t o f th e f ramework and provides th e IoC andDependency Injec t ion f ea t uresT h e basic concep t h ere is th e BeanFac t ory,

    wh ich provides a sop h is t ica t ed implemen t a t iono f th e f ac t ory pa tt ern w h ich removes th e needf or programma t ic single t ons and allows you t odecouple th e con f igura t ion and speci f ica t ion o f

    dependencies f rom your ac t ual program logic

  • 8/8/2019 Spring Basics 1

    20/70

    2 0

    DA O Package

    T h e D A O package provides a JDBC -abs t rac t ion layer th a t removes th e needt o do t edious JDBC coding and parsing

    o f da t abase - vendor speci f ic error codesT h e JDBC package provides a way t o do

    programma t ic as well as declara t ivet ransac t ion managemen t , no t only f orclasses implemen t ing special in t er f aces,bu t f or all your POJOs (plain old Javaobjec t s)

  • 8/8/2019 Spring Basics 1

    21/70

    2 1

    OR M Package

    T h e O R M package provides in t egra t ionlayers f or popular objec t- rela t ionalmapping A PIs, including JP A , JDO,

    Hiberna t e, and iBa t is.Using th e O R M package you can use all

    th ose O/ R- mappers in combina t ion wi th all th e o th er f ea t ures Spring o ff ers, suc h as th e simple declara t ive t ransac t ionmanagemen t f ea t ure men t ionedpreviously

  • 8/8/2019 Spring Basics 1

    22/70

    22

    A OP Package

    Spring ' s A OP package provides an A OPA lliance - complian t aspec t- orien t edprogramming implemen t a t ion allowing you t ode f ine, f or example, me th od - in t ercep t ors andpoin t cu t s t o cleanly decouple codeimplemen t ing f unc t ionali t y th a t s h ould logicallyspeaking be separa t edUsing source - level me t ada t a f unc t ionali t y you

    can also incorpora t e all kinds o f be h avioralin f orma t ion in t o your code

  • 8/8/2019 Spring Basics 1

    23/70

    2 3

    MVC Package

    Spring ' s MVC package provides aModel - View - Con t roller (MVC)implemen t a t ion f or webapplica t ions

    Spring ' s MVC f ramework is no t jus t anyold implemen t a t ion; i t provides a cleansepara t ion be t ween domain model codeand web f orms, and allows you t o use allth e o th er f ea t ures o f th e SpringFramework.

  • 8/8/2019 Spring Basics 1

    24/70

    2 4

    Usage Scenarios

  • 8/8/2019 Spring Basics 1

    25/70

    2 5

    Usage Scenarios

    You can use Spring in all sor t s o f scenarios, f rom apple t s up t o f ully -f ledged en t erprise applica t ions using

    Spring ' s t ransac t ion managemen t f unc t ionali t y and web f rameworkin t egra t ion

  • 8/8/2019 Spring Basics 1

    26/70

    2 6

    T ypical Full -f ledged Spring WebA pplica t ion

  • 8/8/2019 Spring Basics 1

    27/70

    2 7

    Spring Middle -t ier Using 3 rd par t yWeb Framework

  • 8/8/2019 Spring Basics 1

    28/70

    2 8

    R emo t ing Usage Scenario

  • 8/8/2019 Spring Basics 1

    29/70

  • 8/8/2019 Spring Basics 1

    30/70

    3 0

    Dependency Injec t ion (DI):Basic concep t

  • 8/8/2019 Spring Basics 1

    31/70

    3 1

    Spring Dependency Injection

    A kind of Inversion of Control (IoC)Hollywood Principle

    Don't call me, I'll call you

    Container resolves (injects) dependenciesof components by setting implementationobject (push)

    A s opposed to component instantiating or ServiceLocator pattern where component locatesimplementation (pull)

    Martin Fowler calls Dependency Injection

  • 8/8/2019 Spring Basics 1

    32/70

    3 2

    Bene f it s o f Dependency Injec t ion

    FlexibleA void adding lookup code in business logic

    T es t ableNo need t o depend on ex t ernal resources or

    con t ainers f or t es t ing

    Main t ainableA llows reuse in di ff eren t applica t ion environmen t s by

    ch anging con f igura t ion f iles ins t ead o f code

    Promotes a consis

    ten

    tapproac

    hacross allapplica t ions and t eams

  • 8/8/2019 Spring Basics 1

    33/70

    33

    T wo Dependency Injec t ion Varian t s

    Cons t ruc t or dependency Injec t ionDependencies are provided th roug h th e

    cons t ruc t ors o f th e componen t

    Se tt er dependency injec t ionDependencies are provided th roug h th e

    JavaBean s t yle se tt er me th ods o f th ecomponen t

    More popular th an Cons t ruc t or dependencyinjec t ion

  • 8/8/2019 Spring Basics 1

    34/70

    3 4

    Cons t ruc t or Dependency Injec t ion

    public class Cons t ruc t orInjec t ion {priva t e Dependency dep;public Cons t ruc t orInjec t ion(Dependency

    dep) {th is.dep = dep;}

    }

  • 8/8/2019 Spring Basics 1

    35/70

    3 5

    Se tt er Dependency Injec t ion

    public class Se tt erInjec t ion {priva t e Dependency dep;public void se t MyDependency(Dependency

    dep) {th is.dep = dep;}

    }

  • 8/8/2019 Spring Basics 1

    36/70

    3 6

    Dependency Injec t ion (DI):DI Suppor t in Spring

  • 8/8/2019 Spring Basics 1

    37/70

    3 7

    Sub -t opics

    BeanFactory in t er f aceXml BeanFactory implemen t a t ionBean con f igura t ion f ile

    Se tt er dependency injec t ionCons t ruc t or dependency injec t ion

    Beans

    Injec t ion parame t ers

  • 8/8/2019 Spring Basics 1

    38/70

    3 8

    BeanFac t ory

    BeanFac t ory objec t is responsible f ormanaging beans and th eir dependencies

    Your applica t ion in t erac t s wi th Spring ' s DIcon t ainer th roug h BeanFac t ory in t er f ace

    BeanFac t ory objec t h as t o be crea t ed by th eapplica t ion t ypically XmlBeanFac t oryBeanFac t ory objec t , w h en i t ge t s crea t ed, read bean

    conf igura

    tion

    f ile and per

    f orms

    the wiringOnce crea t ed, th e applica t ion can access th e beans

    via BeanFac t ory in t er f ace

  • 8/8/2019 Spring Basics 1

    39/70

    3 9

    BeanFac t ory Implemen t a t ions

    XmlBeanFac t oryConvenience ex t ension o f

    De f aul t Lis t ableBeanFac t oryth

    at

    reads bean def ini

    tions

    f rom an XMLdocumen t

  • 8/8/2019 Spring Basics 1

    40/70

    4 0

    R eading XML Con f igura t ion File via Xml BeanFactory class

    public class XmlConfigWithBeanFactory {

    public static void main(String[] args) {

    XmlBeanFactory factory =

    new XmlBeanFactory( new FileSystemResource( "beans.xml" ));

    SomeBeanInterface b =

    (SomeBeanInterface) factory.getBean(nameOftheBean);

    }

    }

  • 8/8/2019 Spring Basics 1

    41/70

    4 1

    Bean Con f igura t ion File

    E ach bean is defined using tag under the root of the tagT he id attribute is used to give the bean its

    default nameT he cl ass attribute specifies the type of the

    bean

  • 8/8/2019 Spring Basics 1

    42/70

    4 2

    Bean Con f igura t ion File Example

    "htt p://www.spring f ramework.org/d t d/spring - beans.d t d">

    < beans >

    < bean id=" renderer " class =" S t andardOu t Message R enderer ">

    < proper t y name =" messageProvider ">

    < re f local =" provider "/>

    < bean id=" provider " class =" HelloWorldMessageProvider "/>

  • 8/8/2019 Spring Basics 1

    43/70

    4 3

    Wiring a Bean

  • 8/8/2019 Spring Basics 1

    44/70

  • 8/8/2019 Spring Basics 1

    45/70

    45

    Wh a t is Wiring?

    T h e ac t o f crea t ing associa t ions be t weenapplica t ion componen t s is re f erred t o aswiring

    T h ere are many ways t o wire a bean bu t common approac h is via XML

  • 8/8/2019 Spring Basics 1

    46/70

    46

    Wiring example

    "htt p://www.spring f ramework.org/d t d/spring - beans.d t d">

  • 8/8/2019 Spring Basics 1

    47/70

    47

    "htt p://www.spring f ramework.org/d t d/spring - beans.d t d">

  • 8/8/2019 Spring Basics 1

    48/70

    4 8

    Wiring th e beans

    Pro t o t ype and Single t on beansall spring beans are single t onbu t pro t o t ype beans can also be de f ined

    single t on = f alse re t urns a pro t o t ype bean

    singleton =

    true re

    turns a single

    ton beande f aul t value f or single t on is t rue

  • 8/8/2019 Spring Basics 1

    49/70

    4 9

    Wiring th e beans

    Ini t ializa t ion and Des t ruc t ionbeans can be ini t ialized and des t royed bycalling bean speci f ic me th ods

    init-

    meth

    od : calls bean specif ic ini

    tializa

    tionme th od

    des t roy - me th od : calls bean speci f ic cleanupme th od

  • 8/8/2019 Spring Basics 1

    50/70

    5 0

    Wiring th e beans

    Ini t ializa t ion and Des t ruc t ion (example)public class MyConnec t ionPool {

    public void ini t ialize(){ //ini t ialize a connec t ion;

    }public void cleanup() {

    //release connec t ion;}

    }

    con f igura t ion:

  • 8/8/2019 Spring Basics 1

    51/70

    5 1

    Spring Dependency Injec t ionR evisted.

    T wo t ypes o f Dependency Injec t ionse tt er injec t ion

    dependency injec t ed via se tt er me th ods

    cons t ruc t or injec t iondependency injec t ed via cons t ruc t or

  • 8/8/2019 Spring Basics 1

    52/70

    5 2

    Se tt er Injec t ion

    Hello f riends

    Spring Dependency Injec t ion

    Set the greet property bycalling setGreeting ( Hello Friends )

  • 8/8/2019 Spring Basics 1

    53/70

  • 8/8/2019 Spring Basics 1

    54/70

    54

    Cons t ruc t or Injec t ion

    Hello f riends

    Spring Dependency Injec t ion

    constructs a TestBeanobject throughits constructor

  • 8/8/2019 Spring Basics 1

    55/70

    55

    Wiring Collec t ions

    Spring suppor t s Many t ypes o f Collec t ions asbean proper t iesSuppor t ed t ypes are:

    XML Types java.u t il.Lis t , arrays

    java.u t il.Se t

    java.u t il.Map java.u t il.Proper t ies

  • 8/8/2019 Spring Basics 1

    56/70

    56

    Dependency Injec t ion:A u t owiring

  • 8/8/2019 Spring Basics 1

    57/70

    57

    A u t o Wiring

    So f ar we wired beans explici t ly using t agSpring can also do Wiring au t oma t ically

  • 8/8/2019 Spring Basics 1

    58/70

    5 8

    A u t owiring Proper t ies

    Beans may be au t o - wired (ra th er th an using )Per - bean a tt ribu t e autowireExp l icit settings override

    autowire=na m e

    Bean iden t if ier ma t ch es proper t y nameautowire=type

    T ype ma t ch es o th er de f ined bean

    autowire=constructor Ma t ch cons t ruc t or argumen t t ypes

    autowire=autodetect Att emp t by cons t ruc t or, o th erwise t ype

  • 8/8/2019 Spring Basics 1

    59/70

    5 9

    A pplica t ionCon t ex t

  • 8/8/2019 Spring Basics 1

    60/70

    6 0

    Wh a t is A pplica t ionCon t ex t ?

    Ex t ension o f BeanFac t oryI t provides all th e same f unc t ionali t y and moreR educes th e amoun t o f code you needIn a more f ramework - orien t ed s t yle

    A dd new f ea t ures over BeanFac t oryR esource managemen t and accessA ddi t ional li f e - cycle in t er f acesImproved au t oma t ic con f igura t ion o f in f ras t ruc t ure

    componen t sEven t publica t ionIn t erna t ionaliza t ion

  • 8/8/2019 Spring Basics 1

    61/70

    6 1

    Wh en t o Use A pplica t ionCon t ex t ?

    Use A pplicationContext overBeanFactory t o t ake advan t age o f it sex t ended f unc t ionali t y

    Excep t f or a f ew limi t ed si t ua t ions suc h asper h aps in an A pple t , w h ere memoryconsump t ion mig ht be cri t ical, and a f ewex t ra kiloby t es mig ht make a di ff erence

  • 8/8/2019 Spring Basics 1

    62/70

  • 8/8/2019 Spring Basics 1

    63/70

    6 3

    Propaga t ing Even t s

    Even t h andling in th eA pplica t ionCon t ex t is provided th roug h th e A pplica t ionEven t class andA

    pplicationLis

    tener in

    ter

    f aceI f a bean w h ich implemen t s th e

    A pplica t ionLis t ener in t er f ace is deployed in t oth e con t ex t , every t ime an A pplica t ionEven t ge t s publis h ed t o th e A pplica t ionCon t ex t ,th a t bean will be no t if iedEssen t ially, th is is th e s t andard Observer

    design pa tt ern.

  • 8/8/2019 Spring Basics 1

    64/70

    64

    T h ree Buil t- in Even t s

    Con t ex tR e f res h Even tA pplica t ionCon t ex t is ini t ialized or re f res h ed

    Con t ex t ClosedEven t

    A pplica t ionCon t ex t is closedR eques t HandleEven t

    A web - speci f ic even t t elling all beans th a t aHTT P reques t h as been serviced

  • 8/8/2019 Spring Basics 1

    65/70

    65

    Example: Even t Handling

    Con f igura t ion< bean id =" emailer " class =" example.EmailBean ">

    < proper t y name =" blackLis t ">

    < lis t >

    < value > [email protected]

    < value > wh it e@lis t .org

    < value > jo h [email protected]

    < bean id =" blackLis t Lis t ener " class =" example.BlackLis t No t if ier ">< proper t y name =" no t if ica t ion A ddress " value =" spam@lis t .org "/>

  • 8/8/2019 Spring Basics 1

    66/70

  • 8/8/2019 Spring Basics 1

    67/70

    67

    P roperty place h older configurer

  • 8/8/2019 Spring Basics 1

    68/70

    6 8

    How t o use A pplica t ionCon t ex t ?

    Many users will use A pplica t ionCon t ex t in a comple t ely declara t ive f as h ion, no t even h aving t o crea t e i t manually, bu t

    instead relying on suppor

    tclasses suc

    h as Con t ex t Loader t o au t oma t ically s t ar t

    an A pplica t ionCon t ex t as par t o f th enormal s t ar t up process o f a J 2 EE

    webappit is s t ill possible t o programma t icallycrea t e an A pplica t ionCon t ex t

    A l

  • 8/8/2019 Spring Basics 1

    69/70

    6 9

    Spring A pplica t ionCon t ex t

    A Spring A pplica t ionCon t ex t allows you t o ge t accesst o th e objec t s th a t are con f igured in a BeanFac t ory ina f ramework manner.A pplica t ionCon t ex t ex t ends BeanFac t ory

    A dds services suc h as in t erna t ional messagingcapabili t ies.A dd th e abili t y t o load f ile resources in a genericf as h ion.

    Several ways t o con f igure a con t ex t :X MLWeb A pplicationContext Con f igura t ion f or aweb applica t ion.Class P at h X ML A pplicationContext s t andalone XMLapplica t ion con t ex tFileSystem X ml A pplicationContext

    A llows you t o avoid wri t ing Service Loca t ors

  • 8/8/2019 Spring Basics 1

    70/70

    7 0

    Summary

    Using dependency injec t ion we caninjec t th e dependencies in t o ourapplica t ion

    IOC reduces th e programmers burdenDiff eren t t ypes o f wring gives us moref lexibili t y