Introducing Spring Framework
Who am I ?
Developing Software
“I’m not a great programmer, I’m just a good
programmer with great habits.”
                                      Kent Beck
Our toolbox


      • Clean code
      • TDD
      • Design patters
      • Frameworks
      • ...
Clean code
Source code are for humans not computers!




                               “Code Smells”, Martin Fowler
TDD
Design Patterns
Frameworks
Choose your weapon !
Spring Framework
The Spring Framework



• Best practices
• Proven Solutions
• OpenSource
• Continuously growing
Spring Ecosystem

                  Spring MVC            Spring
                                        WebFlow

 Spring Dynamic                                   Spring ROO
     Modules


                                                    Spring Web
Spring Security                                      Services
                                 Spring
                               Framework
                                                        Spring
 Spring Batch                                         Integration


                                                   Spring Data
   Spring Social

                       Spring Android      And many more...
In a Nutshell

• IoC container
• Lightweight
• Comprehensive coverage of AOP
• TDD
• Modular
Where?

• Junit system Test
• Java EE web application
• Java EE enterprise application
• Standalone Java application
What?
Spring Framework provides comprehensive
infrastructure support for developing Java
applications.
   −Spring   deals with the plumbing
   −So   you can focus on solving the domain problem.
And that means...

You can build applications from “plain old Java
objects” (POJOs) and to apply enterprise services
non-invasively to POJOs.
Spring IoC
Inversion of Control (IoC)

“Is a programming technique, in which object coupling
is bound at run time by an assembler object and is
typically not known at compile time using static
analysis.”
How?
Spring Triangle




                       As
                          p
                   n



                         ec
                io




                            t-O
              ct
            je




                               rie
          In




                               nt
           y
        nc




                                 ed
               Simple
     de




                                     Pr
      n




                                       og
   pe




               Object

                                         ra
De




                                           m
                                          m
                                           in
                                              g
 Enterprise Service Abstractions
Production
Unit Test




   Stub / Mock
Local development




            H2
Deployments


Development      Test           QA          Production




   or             +              +              +
 Mocks        Dummy data   Dump real data   Real data
Dependency Injection (IoC)


  • XML
  • Annotations
  • Java
TransferService
AccountRepository
XML




      app-config.xml
And run it !
Annotations
XML




      app-config.xml
And run it !
Java Configuration
                Defined outside !!
Spring AOP
Aspect-Oriented Programming

“AOP is a programming paradigm that aims to increase
modularity by allowing the separation of cross-cutting
concerns”
Cross-Cutting Concerns?
Generic funcionality that is needed in many
places in your application


   −   Security
   −   Caching
   −   Logging
   −   ...
Tangling




           Mixing of concerns
Scattering




             Duplication
How?

                       AOP




                                                           <<Interface>>
                                                          TransferService

TransferServiceImpl
      (Target)
                                                      Spring AOP Proxy

                                  transfer(300,1,2)

                                                        Aspect          Target
       ExceptionReporterAdvice
               (Aspect)
With Spring AOP


                                                                          TransferServiceImpl.java
                                                                           TransferServiceImpl.java




                                                                        ExceptionReporterAdvice.java
                                                                        ExceptionReporterAdvice.java

<beans>
  <aop:aspectj-autoproxy>
    <aop:include name=“exceptionReporter” />
  </aop:aspectj-autoproxy>

   <bean id=“exceptionReporter” class=“...ExceptionReporterAdvice” />
</beans>
                                                                               Spring-aop.xml
                                                                               Spring-aop.xml
What about ... ?
Web Access




HTTP / HTML
Spring Web Integration

• Spring provides support in the Web layer
  − Spring MVC, Spring WebFlow, …



• However, you are free to use Spring with any Java
web framework
  − Struts,Tapestry, JSF, WebWork, Wicket, ...
web.xml

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
     /WEB-INF/spring/app-config.xml
   </param-value>
</context-param>

<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>
Spring MVC
Model - View - Controller
Spring MVC
                                                              @Controller
                                                             BlazeDS (Flex)
                                                                WebFlow
request                                                            ...
                                                                    HandlerMapping
                                                                     HandlerMapping
           DispatcherServlet                       Handler          HandlerAdapter
                                                                     HandlerAdapter

response                            model + View

                        render(model)
           JSP


                 View
                          JSP
                          PDF
                          Excel
                           ...
                                ViewResolver
                                 ViewResolver
Spring MVC
Quick Start

1. Deploy a Dispatcher Servlet
2. Implement a request Handler (Controller)
3. Implement the View
4. Register the Controller
5. Deploy and Test
Deploy Dispatcher Servlet
                                                                          web.xml
<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>
          /WEB-INF/spring/mvc-config.xml
     </param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>/app/*</url-pattern>
</servlet-mapping>




                                                Don't forget ContextLoaderListener
                                                          (app-config.xml)
Implement Controller
Register Controller

<beans>

  <bean class=”org.spf.web...InternalResourceViewResolver”>
    <property name=”prefix” value=”/WEB-INF/views/” />
    <property name=”suffix” value=”.jsp” />
  </bean>

  <bean class=”com.extrema...TransferController” />

</beans>




                                                      /WEB-INF/spring/mvc-config.xml
Implement View
<html>
 <head><title>Your transfer</title></head>
 <body>
    <h1>Your transfer </h1>
    Id = ${transfer.id} <br/>
    Date = ${transfer.date} <br/>
    Amount = ${transfer.amount} <br/>
    Credit = ${transfer.credit} <br/>
    Debit = ${transfer.debit} <br/>
  </body>
</html>




                                             /WEB-INF/views/transfer.jsp
Deploy and Test
http://localhost:8080/app/transfer/1




    Your transfer
         Id = 1
         Date = 2013/01/01
         Amount = 300.0
         Credit = 1234
         Debit = 5678
… and much more
• Stateless converter for binding and formatting
• Support for JSR-303 declarative validation
• I18n
• Themes
• Content Negotiation
• Support for RESTful web services
• WebFlow
Summary
Conclusions

• Developing software is a craft.
• We need tools to help us in the process.
   −Good   practices
   −Frameworks

• It depends on you to choose the right one for your needs.
• Spring is just one of them.
Q&A
Ernesto Hernández
           @ehdez73

Introducing spring

  • 1.
  • 2.
  • 3.
  • 4.
    “I’m not agreat programmer, I’m just a good programmer with great habits.” Kent Beck
  • 5.
    Our toolbox • Clean code • TDD • Design patters • Frameworks • ...
  • 6.
    Clean code Source codeare for humans not computers! “Code Smells”, Martin Fowler
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
    The Spring Framework •Best practices • Proven Solutions • OpenSource • Continuously growing
  • 13.
    Spring Ecosystem Spring MVC Spring WebFlow Spring Dynamic Spring ROO Modules Spring Web Spring Security Services Spring Framework Spring Spring Batch Integration Spring Data Spring Social Spring Android And many more...
  • 14.
    In a Nutshell •IoC container • Lightweight • Comprehensive coverage of AOP • TDD • Modular
  • 15.
    Where? • Junit systemTest • Java EE web application • Java EE enterprise application • Standalone Java application
  • 16.
    What? Spring Framework providescomprehensive infrastructure support for developing Java applications. −Spring deals with the plumbing −So you can focus on solving the domain problem.
  • 17.
    And that means... Youcan build applications from “plain old Java objects” (POJOs) and to apply enterprise services non-invasively to POJOs.
  • 18.
  • 19.
    Inversion of Control(IoC) “Is a programming technique, in which object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis.”
  • 20.
  • 21.
    Spring Triangle As p n ec io t-O ct je rie In nt y nc ed Simple de Pr n og pe Object ra De m m in g Enterprise Service Abstractions
  • 22.
  • 23.
    Unit Test Stub / Mock
  • 24.
  • 25.
    Deployments Development Test QA Production or + + + Mocks Dummy data Dump real data Real data
  • 26.
    Dependency Injection (IoC) • XML • Annotations • Java
  • 27.
  • 28.
  • 29.
    XML app-config.xml
  • 30.
  • 31.
  • 32.
    XML app-config.xml
  • 33.
  • 34.
    Java Configuration Defined outside !!
  • 35.
  • 36.
    Aspect-Oriented Programming “AOP isa programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns”
  • 37.
    Cross-Cutting Concerns? Generic funcionalitythat is needed in many places in your application − Security − Caching − Logging − ...
  • 38.
    Tangling Mixing of concerns
  • 39.
    Scattering Duplication
  • 40.
    How? AOP <<Interface>> TransferService TransferServiceImpl (Target) Spring AOP Proxy transfer(300,1,2) Aspect Target ExceptionReporterAdvice (Aspect)
  • 41.
    With Spring AOP TransferServiceImpl.java TransferServiceImpl.java ExceptionReporterAdvice.java ExceptionReporterAdvice.java <beans> <aop:aspectj-autoproxy> <aop:include name=“exceptionReporter” /> </aop:aspectj-autoproxy> <bean id=“exceptionReporter” class=“...ExceptionReporterAdvice” /> </beans> Spring-aop.xml Spring-aop.xml
  • 42.
  • 43.
  • 44.
    Spring Web Integration •Spring provides support in the Web layer − Spring MVC, Spring WebFlow, … • However, you are free to use Spring with any Java web framework − Struts,Tapestry, JSF, WebWork, Wicket, ...
  • 45.
    web.xml <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/app-config.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
  • 46.
  • 47.
    Model - View- Controller
  • 48.
    Spring MVC @Controller BlazeDS (Flex) WebFlow request ... HandlerMapping HandlerMapping DispatcherServlet Handler HandlerAdapter HandlerAdapter response model + View render(model) JSP View JSP PDF Excel ... ViewResolver ViewResolver
  • 49.
  • 50.
    Quick Start 1. Deploya Dispatcher Servlet 2. Implement a request Handler (Controller) 3. Implement the View 4. Register the Controller 5. Deploy and Test
  • 51.
    Deploy Dispatcher Servlet web.xml <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/mvc-config.xml </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/app/*</url-pattern> </servlet-mapping> Don't forget ContextLoaderListener (app-config.xml)
  • 52.
  • 53.
    Register Controller <beans> <bean class=”org.spf.web...InternalResourceViewResolver”> <property name=”prefix” value=”/WEB-INF/views/” /> <property name=”suffix” value=”.jsp” /> </bean> <bean class=”com.extrema...TransferController” /> </beans> /WEB-INF/spring/mvc-config.xml
  • 54.
    Implement View <html> <head><title>Yourtransfer</title></head> <body> <h1>Your transfer </h1> Id = ${transfer.id} <br/> Date = ${transfer.date} <br/> Amount = ${transfer.amount} <br/> Credit = ${transfer.credit} <br/> Debit = ${transfer.debit} <br/> </body> </html> /WEB-INF/views/transfer.jsp
  • 55.
    Deploy and Test http://localhost:8080/app/transfer/1 Your transfer Id = 1 Date = 2013/01/01 Amount = 300.0 Credit = 1234 Debit = 5678
  • 56.
    … and muchmore • Stateless converter for binding and formatting • Support for JSR-303 declarative validation • I18n • Themes • Content Negotiation • Support for RESTful web services • WebFlow
  • 57.
  • 58.
    Conclusions • Developing softwareis a craft. • We need tools to help us in the process. −Good practices −Frameworks • It depends on you to choose the right one for your needs. • Spring is just one of them.
  • 59.
  • 60.