Docstoc

Spring Framework

Document Sample
Spring Framework Powered By Docstoc
					       Spring Framework


            Rafael A. Calvo

            Web Engineering Group
School of Electrical and Information Engineering
       University of Sydney, NSW 2006



              August, 2009
Table of Contents

         History

         Dependency Injection

         The basics - web applications

         Toy Application: Bibliography
   4.1   Wiring Beans
   4.2   JSTL: JavaServer Pages Standard Tag Library
   4.3   Servlets context
   4.4   BeanFactory and ApplicationContext

         Integration with Hibernate

         Activity

         Design Patterns

         Further Information

         Bibliography

     August, 2009                                2008 by Rafael A. Calvo   2
History




     1. Problems with EJB
     2. Lightweight containers appear
     3. 2002/2003 Spring is created by Rod Johnson and Juergen Hoeller
     4. Expert one-on-one J2EE Design and Development is published with the
        code that became Spring (2003)
     5. Spring 1.0 Released March 2004
     6. 2004/2005 Spring is recognized as leading full-stack Java/J2EE
        application framework




      August, 2009                                2008 by Rafael A. Calvo     3
Dependency Injection I



     A simple Plain Old Java Object (POJO) with setter and getter methods:
 0     import j a v a . i o . S e r i a l i z a b l e ;
     p u b l i c c l a s s Product implements S e r i a l i z a b l e {
             private String description ;
             p r i v a t e D ou bl e p r i c e ;
             public void s e t D e s c r i p t i o n ( String s ) {
 5                   description = s ;
            }
             public String getDescription () {
                     return description ;
            }
10           p u b l i c v o i d s e t P r i c e ( Double d ) {
                     price = d;
            }
             p u b l i c Do ub le g e t P r i c e ( ) {
                     return price ;
15          }
     }




        August, 2009                                            2008 by Rafael A. Calvo   4
Dependency Injection I




     The object gets all its dependencies injected in a context configuration file:
 0   <b e a n s>
     <bean i d=" product1 " c l a s s=" bus . Product ">
                                                          <v                           </
                  <p r o p e r t y name=" description "> a l u e>Lamp</ v a l u e> p r o p e r t y>
                                                  <v                            </
                  <p r o p e r t y name=" price "> a l u e>5 . 7 5</ v a l u e> p r o p e r t y>
           </ bean>
 5         <bean i d=" product2 " c l a s s=" bus . Product ">
                                                          <v                             </
                  <p r o p e r t y name=" description "> a l u e>T a b l e</ v a l u e> p r o p e r t y>
                                                  <v                             </
                  <p r o p e r t y name=" price "> a l u e>7 5 . 2 5</ v a l u e> p r o p e r t y>
           </ bean>
     </ b e a n s>




        August, 2009                                             2008 by Rafael A. Calvo                   5
The basics - web applications




                     Figure: Overview of the Spring Framework (from [1])




      August, 2009                                     2008 by Rafael A. Calvo   6
The basics - web applications


     Request life cycle
                                                                1. Browser sends request, received by
                            HandlerMapping
                                                                   DispatcherServlet (front
                                                                   controller)
                     2

                                    3
                                                                2. To figure which controller to use,
 1                                                Controller
                                                                   as HandlerMapping
        DispatcherServlet
                            ModelAndView
                                             4                  3. Dispacther server requests
                                                                   Controller
                                        5
                                                 Viewresolver   4. Controller returns ModelAndView
                                6                                  object
                                 View
                                                                5. If ModelAndView contains logical
                                                                   name for a View,
                                                                   DispatcherServlet queries
     Figure: The life cycle of a request. Based                    ViewResolver for the View object
     on [4]                                                        that will render the response
                                                                6. DispatcherServlet requests the
                                                                   View Object.

          August, 2009                                              2008 by Rafael A. Calvo             7
The basics - web applications

  File Organization




                                                      src: all business logic files
                                                      war: everything to be deployed




          Figure: Eclipse Screenshot



    To start you only need a JSP file for users to see and the web.xml configuration file.
       August, 2009                                    2008 by Rafael A. Calvo            8
The basics - web applications




  Layers and configuration

     Web (presentation)
                          bibliography-servlet.xml
           Layer

        Service Layer     bibliography-service.xml


      Persistence Layer   bibliography-data.xml



  Figure: Dividing a Spring application in
  layers




        August, 2009                                 2008 by Rafael A. Calvo   9
Toy Application: Bibliography




   The Bibliography application manages bibliographical references. It allows
   users to create and edit entries. It also adds information from Google
   webservices and uses text mining techniques for more elaborate functionalities.
   We will use the basic functionalities.
     1. web.xml configuration file
     2. hello.jsp user interface file
     3. bibliography-servlet.xml




     August, 2009                                 2008 by Rafael A. Calvo            10
Wiring Beans I


                        Listing 1: ’war/WEB-INF/web.xml configuration file’
     <? xml v e r s i o n=" 1.0 " e n c o d i n g=" ISO -8859 -1 " ?>
     <!DOCTYPE web−app PUBLIC " -// Sun Microsystems , Inc .// DTD Web Application 2.3
               " http: // java . sun . com // dtd / web - app_2_3 . dtd ">
     <web−app>
15                    −
               <!− b a s i c c o n t e x t − >          −
               < s e r v l e t>
                              <s e r v l e t −name>b i b l i o g r a p h y</ s e r v l e t −name>
                              <s e r v l e t −c l a s s>
                               o r g . s p r i n g f r a m e w o r k . web . s e r v l e t . D i s p a t c h e r S e r v l e t
20                            </ s e r v l e t −c l a s s>
                              <l o a d −on−s t a r t u p>1</ l o a d −on−s t a r t u p>
               </ s e r v l e t>
               <s e r v l e t −mapping>
                              <s e r v l e t −name>b i b l i o g r a p h y</ s e r v l e t −name>
25                            <u r l −p a t t e r n>∗ . htm</ u r l −p a t t e r n>
               </ s e r v l e t −mapping>
               <welcome−f i l e − l i s t>
                              <welcome− f i l e>i n d e x . j s p</ welcome− f i l e>
               </ welcome−f i l e − l i s t>
30             < t a g l i b>
                              <t a g l i b −u r i>/ s p r i n g</ t a g l i b −u r i>
                              <t a g l i b −l o c a t i o n>/WEB        −INF / s p r i n g . t l d</ t a g l i b −l o c a t i o n>
               </ t a g l i b>


        August, 2009                                                    2008 by Rafael A. Calvo                              11
Wiring Beans II


      Listing 2: ’src/weg/bibliography/mvc/ListReferenceController.java Controller’
 0   p u b l i c c l a s s L i s t R e f e r e n c e C o n t r o l l e r extends A b s t r a c t C o n t r o l l e r {
            /∗∗ L o g g e r f o r t h i s c l a s s and s u b c l a s s e s ∗/
             p r o t e c t e d f i n a l Log l o g g e r = L o g F a c t o r y . g e t L o g ( g e t C l a s s ( ) ) ;
             private ReferenceService referenceService ;                                     // s e t t e r i n j e c t e d
             p u b l i c ModelAndView h a n d l e R e q u e s t I n t e r n a l (
 5                            HttpServletRequest request , HttpServletResponse response )
                             throws Exception {
                     S t r i n g now = ( new j a v a . u t i l . Date ( ) ) . t o S t r i n g ( ) ;
                         Map model = new HashMap ( ) ;
                     model . p u t ( " now " , now ) ;
10                   model . p u t ( " references " , r e f e r e n c e S e r v i c e . g e t R e f e r e n c e s ( ) ) ;
                     // l o g g e r . i n f o ( ” v i e w i n g NOW: ” + now ) ;
                     r e t u r n new ModelAndView ( " lis trefere nces " , " model " , model ) ;
            }
             public void setReferenceService ( ReferenceService s e r v i c e ) {
15                   this . referenceService = service ;
            }
     }




        August, 2009                                                       2008 by Rafael A. Calvo                            12
Wiring Beans III


                        Listing 3: ’war/WEB-INF/jsp/listreferences.jsp file’
 0   <%@ i n c l u d e f i l e =" / WEB - INF / jsp / include . jsp " %          >
     <p><fmt : m e s s a g e k e y=" greeting " /> <c : o u t v a l u e=" $ { model . now } " />
                 <t a b l e b o r d e r =0 c e l l p a d d i n g =2 c e l l s p a c i n g =2>
                                                              <t                 <t                   <t
                       <t r b g c o l o r=" # cccccc "> d>ID</ t d> d>A u t h o r</ t d> d>T i t l e</ t d>                 <t
                       <c : f o r E a c h v a r=" refs " i t e m s=" $ { model . references } ">
 5                             <t r b g c o l o r=" # eeeeee ">
                                            <c
                                     <t d> : o u t v a l u e=" $ { refs . id } " /> </ t d>
                                            <c
                                     <t d> : o u t v a l u e=" $ { refs . author } " /> </ t d>
                                            <c
                                     <t d> : o u t v a l u e=" $ { refs . title } " /> t d>    </
                                     <t d>  <fmt : f o r m a t D a t e v a l u e=" $ { refs . timestamp } " t i m e S t y l e=
10                                   <t d>                                                    <c
                                            <a h r e f=e d i t r e f e r e n c e . htm ? i d= : o u t v a l u e=" $ { refs . id
                               </ t r>
                       </ c : f o r E a c h>
                 </ t a b l e>
                 <p>
15               < %@ i n c l u d e f i l e =" / WEB - INF / jsp / footer . jsp " %          >


                Guess: how would the include file look like?




        August, 2009                                                  2008 by Rafael A. Calvo                             13
JSTL: JavaServer Pages Standard Tag Library




    1. JSTL core: if, forEach, out, set...
    2. JSTL fmt: setLocale, bundle, setBundle, formatDate,...
    3. JSTL sql: transaction, query, update, param
    4. JSTL XML:out, if, forEach, ...
    5. JSTL functions: contains(), containsIgnoreCase(),length()...




     August, 2009                                2008 by Rafael A. Calvo   14
Servlets context I
     The web.xml file defined URL patterns. All URLs matching those patterns are
     routed to the Spring DispatchetServlet and picked by one or more
     HandlerMappings. There are three handler mapping classes:
     BeanNameUrlHandlerMapping, SimpleUrlHandlerMapping,
     CommonsPathMapUrlHandlerMapping.

                         WEB-INF/bibliography-servlet.xml configuration file
 0   <? xml v e r s i o n=" 1.0 " e n c o d i n g=" UTF -8 " ?>
     <!DOCTYPE b e a n s PUBLIC " -// SPRING // DTD BEAN // EN "
      " http: // www . springfram ew o rk . org / dtd / spring - beans . dtd ">
         −
     <!− A p p l i c a t i o n c o n t e x t f o r " bibliography " D i s p a t c h e r S e r v l e t − >           −
     <b e a n s>
 5         <bean i d=" b i b l i o g r a p h y C o n t r o l l e r " c l a s s=" b i b l i o g r a p h y C o n t r o l l e r " />
           <bean i d=" urlMapping " c l a s s="
                 org . springframew or k . web . servlet . handler . S i m p l e U r l H a n d l e r M a p p i n g "
                 <p r o p e r t y name=" mappings ">
                       <p r o p s>
10                             <p r o p k e y=" / addreference . htm ">
                                   A d d R e f e r e n c e C o n t r o l l e r</ p r o p>
                               <p r o p k e y=" / editreference . htm ">
                                   E d i t R e f e r e n c e C o n t r o l l e r</ p r o p>
                               <p r o p k e y=" / upload . htm ">
15                                 F i l e U p l o a d C o n t r o l l e r</ p r o p>
                       </ p r o p s>
                 </ p r o p e r t y>
           </ bean>
        August, 2009                                                       2008 by Rafael A. Calvo                                  15
Servlets context II
                  −
            <!− More e l a b o r a t e m a p p i n g s − >       −
20     <bean i d=" be a nN a me U r l M a p p i n g "
             c l a s s=
            " org . springframe w or k . web . servlet . handler . B e a n N a m e U r l H a n d l e r M a p p i n g "
           −
       <!− T a b l e L i s t i n g − >  −
       <bean name=" / listrefe rences . htm "
25           c l a s s=" weg . bibliography . mvc . L i s t R e f e r e n c e C o n t r o l l e r ">
            <p r o p e r t y name=" r e f e r e n c e S e r v i c e ">
                < r e f bean=" re f e r e n c e S e r v i c e " />
            </ p r o p e r t y>
       </ bean>
30                −
            <!− S i m p l e F o r m C o n t r o l l e r t h a t manage t h e add / e d i t R e f e r e n c e − >   −
       <bean i d=" A d d R e f e r e n c e C o n t r o l l e r "
             c l a s s=" weg . bibliography . mvc . A d d R e f e r e n c e C o n t r o l l e r ">
            <p r o p e r t y name=" r e f e r e n c e S e r v i c e " r e f=" r e f e r e n c e S e r v i c e " />
            <p r o p e r t y name=" formView " v a l u e=" editreference " />
35          <p r o p e r t y name=" validator " r e f=" R e f e r e n c e V a l i d a t o r " />
            <p r o p e r t y name=" successView " v a l u e=" li strefere nces " />
       </ bean>
      <bean name=" / editreference . htm " i d=" E d i t R e f e r e n c e C o n t r o l l e r "
                           c l a s s=" weg . bibliography . mvc . E d i t R e f e r e n c e C o n t r o l l e r ">
40          <p r o p e r t y name=" r e f e r e n c e S e r v i c e " r e f=" r e f e r e n c e S e r v i c e " />
            <p r o p e r t y name=" formView " v a l u e=" editreference " />
            <p r o p e r t y name=" validator " r e f=" R e f e r e n c e V a l i d a t o r " />
            <p r o p e r t y name=" successView " v a l u e=" li strefere nces " />
      </ bean>
45                −
            <!− V a l i d a t o r i n t e r f a c e f o r R e f e r e n c e f o r m s − >  −
      <bean i d=" Re f er e nc eV a l i d a t o r "
             c l a s s=" weg . bibliography . mvc . R e f e r e n c e V a l i d a t o r " />
     August, 2009                                                2008 by Rafael A. Calvo                           16
Servlets context III



                  −
             <!− View and i n t e r n a t i o n a l i z a t i o n / r e s o u r c e b u n d l e m a p p i n g s− >    −
             <bean i d=" viewResolver "
50                    c l a s s="
                      org . springfra m ew or k . web . servlet . view . I n t e r n a l R e s o u r c e V i e w R e s o l v e r "
                 <p r o p e r t y name=" viewClass ">
                   <v a l u e>
                        o r g . s p r i n g f r a m e w o r k . web . s e r v l e t . v i e w . J s t l V i e w
55                             </
                 </ v a l u e> p r o p e r t y>
                 <p r o p e r t y name=" prefix "> a l u e>/WEB<v                  −INF / j s p /</ v a l u e> p r o p e r t y>
                                                                                                                  </
                                                               <v
                 <p r o p e r t y name=" suffix "> a l u e>. j s p</ v a l u e> p r o p e r t y>   </
             </ bean>
             <bean i d=" messageSource "
60                      c l a s s="
                        org . springfra m ew or k . context . support . R e s o u r c e B u n d l e M e s s a g e S o u r c e ">
                                                                  <v
                 <p r o p e r t y name=" basename "> a l u e>m e s s a g e s</ v a l u e> p r o p e r t y>     </
             </ bean>
     </ b e a n s>




        August, 2009                                                    2008 by Rafael A. Calvo                              17
Toy Application: Bibliography I




   The org.springframework.beans.factory.BeanFactory interface manages
   the application’s configuration normally defined in the XML files of Listings ??.
   The org.springframework.context.ApplicationContext adds resources
   such as resource bundles (normally used for internationalization), event
   broadcasting, etc.




     August, 2009                                2008 by Rafael A. Calvo            18
Activity




    The activity is based on the 4-part Spring MVC tutorial contained with the
    Spring distribution. The activity reviews what we have seen here, and guides
    you creating a simple Spring application [3].
     1. Open: spring-framework/docs/MVC-step-by-step/Spring-MVC-step-by-step.html
     2. Follow the instructions and see how far you can get in the time allocated. The
        tutorial has 4 parts. You can complete all in your own time.
     3. Implement a storage mechanism, based on Hibernate




      August, 2009                                   2008 by Rafael A. Calvo             19
Design Patterns




    1. Model View Controller (MVC). This pattern is used in most web
       applications, and is the point of Spring MVC.
    2. Front Controller. Having a Dispatcher Servlet. Key to MVC




     August, 2009                              2008 by Rafael A. Calvo   20
Further Information




     1. The Spring site itself: www.spring-framework.org/
     2. The ”Spring in Action” book [4], a clear introduction to Spring.
     3. ”Agile development ..” [2]
     4. and of course the course page:
         http://weg.ee.usyd.edu.au/people/rafa/sakai




     August, 2009                                 2008 by Rafael A. Calvo   21
Bibliography



         Rod Johnson; Juergen Hoeller; et. al.
         Spring reference manual.
         Technical report, Spring Framework Project, version 1.2.6.

         Anil Hemrajani.
         Agile Java Development with Spring Hibernate and Eclipse.
         Sams Publishing, 2006.

         Thomas Risberg.
         Developing a spring framework mvc application step-by-step.
         Technical report, Spring Framework Project, 2005.

         Craig Walls and Ryan Breidenbach.
         Spring in Action.
         Manning Publications Company, 2005.




     August, 2009                                     2008 by Rafael A. Calvo   22
Questions, Comments, Acknowledgements




   I would like to thank the developers of all these open source frameworks for
   their contribution.


                                 Questions?




     August, 2009                                 2008 by Rafael A. Calvo         23

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:88
posted:4/4/2010
language:English
pages:23
Description: Spring Framework