Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Making Spring Boot Groovier

graemerocher
September 09, 2014

Making Spring Boot Groovier

Talk by Graeme Rocher on embracing Groovy throughout your Spring Boot codebase from SpringOne2GX 2014

graemerocher

September 09, 2014
Tweet

More Decks by graemerocher

Other Decks in Programming

Transcript

  1. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Making Spring Boot Groovier
    By Graeme Rocher

    View Slide

  2. Agenda
    § Spring Boot & Groovy
    § Adding a little bit of Groovy
    § Going nuts with Groovy
    § Testing with Groovy
    § Summary / Q & A

    View Slide

  3. Spring Boot & Groovy
    • Spring Boot not dependent on Groovy
    – …but already has Groovy!
    • Spring Boot Groovy components
    – Spring Boot CLI
    – Spring Groovy Templates
    – Spring Boot Autoconfigure
    3
    3.0

    View Slide

  4. Spring Boot Micro Services
    • Already expressed more concisely with Groovy
    4
    @Controller  
    class  ThisWillActuallyRun  {  
           @RequestMapping("/")  
           @ResponseBody  
           String  home()  {  
                   "Hello  World!"  
           }  
    }

    View Slide

  5. Adding Groovy to Spring Boot
    • Starting slow
    – Gradle for the build
    – Spock for unit testing
    • Digging deeper
    – Groovy controllers
    – Groovy templates
    • Going Groovy all the way!
    – GORM
    – GSP
    5
    3.0

    View Slide

  6. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Adding a powerful

    build system
    Step 1: Gradle

    View Slide

  7. Gradle
    • Gradle is…
    – A flexible powerful build tool
    – based on a Groovy DSL
    – well documented
    – used by Android
    – better than Maven ;-)
    • More at http://www.gradle.org/documentation
    7

    View Slide

  8. Why Gradle?
    • Builds are not static
    • Flexibility is key
    • Simple plugin model
    • Android compatible
    • Constantly innovating
    8

    View Slide

  9. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Demo: Gradle

    View Slide

  10. Gradle
    • Just add build.gradle!
    10
    apply  plugin:"groovy"  
    …  
    dependencies  {  
           compile("o.s.b:spring-­‐boot-­‐starter-­‐
    web:1.1.6.RELEASE")  
    }

    View Slide

  11. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Taking Spring Boot

    testing to the next level
    Step 2: Spock

    View Slide

  12. Spock Framework
    • Spock is…
    – test & specification framework
    – based on a Groovy DSL
    – well documented
    – powerful and intuitive
    – better than JUnit ;-)
    • See http://docs.spockframework.org/en/latest/
    12

    View Slide

  13. Why Spock?
    • Data driven tests
    • Integrated mocking / stubbing
    • Power assertions
    13
    def  x  =  [1,  2,  3]  
    assert  x  ==  [3,  2,  1]          
    !
    Caught:  Assertion  failed:  
    assert  x  ==  [3,  2,  1]  
                 |  |  
                 |  false  
                 [1,  2,  3]

    View Slide

  14. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Demo: Spock

    View Slide

  15. Spock
    • Add spock as a test scoped dependency
    !
    !
    !
    • Then just extend spock.lang.Specification
    15
    testCompile(  

       "[..]:spock-­‐core:0.7-­‐groovy-­‐2.0"  )
    class  FooSpec    
                     extends  spock.lang.Specification  

    View Slide

  16. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Embrace Groovy throughout

    your Spring Boot codebase
    Step 3: Groovy Everywhere

    View Slide

  17. Groovy Everywhere
    • Groovy & Java can be seamlessly
    mixed
    • With @CompileStatic there is no
    longer a performance cost
    • Introducing Groovy into your
    codebase is easier than ever!
    17
    3.0

    View Slide

  18. Why Groovy?
    • Get Java 8 lambdas but deployable to
    any JVM (1.5+)
    • Easy to learn for Java developers
    • Static or dynamic compilation
    • Android support
    • Extensive Groovy SDK
    18
    3.0

    View Slide

  19. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Demo: Groovy Controllers

    View Slide

  20. Groovy Spring Controllers
    • Just write your controller in Groovy
    20
    @RestController  
    class  ExampleController  {  
           @RequestMapping("/")  
           String  hello()  {  
                   "Hello  World!"  
           }  
    }

    View Slide

  21. Groovy Spring Controllers
    • Add @CompileStatic if you want type checking
    21
    @RestController  
    @CompileStatic  
    class  ExampleController  {  
           @RequestMapping("/")  
           String  hello()  {  
                   "Hello  World!"  
           }  
    }

    View Slide

  22. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Enjoy writing views again

    with Groovy templates
    Step 4: Groovy Templates

    View Slide

  23. Groovy Templates
    • MarkupTemplateEngine introduced in

    Groovy 2.3
    • Support for type safe views,

    layouts etc.
    • Markup builder style and native
    Spring Boot support
    23
    3.0

    View Slide

  24. Groovy Templates
    • Add groovy-templates as a dependency
    !
    !
    !
    !
    • Then add Groovy templates with file extension .tpl to
    src/main/templates/views !
    • Layouts go into src/main/templates/layouts
    24
    compile  "org.codehaus.groovy:groovy-­‐
    templates:2.3.6"

    View Slide

  25. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Demo: Groovy Templates

    View Slide

  26. Groovy Templates
    • Elegant, readable views expressed in Groovy!
    26
    ul  {  
       persons.each  {  person  -­‐>  
           li  {  
               a(href:  "/person/$person.id",  
    "$person.lastName  $person.firstName")  
           }  
       }  
    }

    View Slide

  27. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Next level persistence with
    GORM
    Step 5: GORM

    View Slide

  28. GORM
    • Powerful multi-datastore
    query layer
    • Dynamic finders, criteria, and
    persistence methods
    • Integrated validation
    • Automatic mapping of entities
    to underlying datastore
    • Easy access to native APIs
    28

    View Slide

  29. GORM for Hibernate
    • Add gorm-hibernate4-spring-boot as a dependency
    !
    !
    !
    !
    • Then add persistent entities that are annotated with 

    grails.persistence.Entity
    29
    compile  "org.grails:gorm-­‐hibernate4-­‐
    spring-­‐boot:1.0.0.RELEASE"

    View Slide

  30. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Demo: GORM for Hibernate

    View Slide

  31. GORM for Hibernate
    • Trivial to create entities
    31
    import  grails.persistence.*  
    !
    @Entity  
    class  Person  {  
           String  firstName  
           String  lastName  
    }

    View Slide

  32. GORM for Hibernate
    • Powerful query composition with detached criteria
    32
    def  employees  =  Employee.where  {  
           region.continent  in  ['APAC',  "EMEA"]  
    }.id()  
    !
    def  results  =  Sale.where  {  
         employee  in  employees  &&  total  >  50000  
    }.employee.list()

    View Slide

  33. GORM for MongoDB
    • Add gorm-mongodb-spring-boot as a dependency
    !
    !
    !
    !
    • Then add persistent entities that are annotated with 

    grails.persistence.Entity
    33
    compile  "org.grails:gorm-­‐mongodb-­‐spring-­‐
    boot:1.1.0.RELEASE"

    View Slide

  34. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Demo: GORM for MongoDB

    View Slide

  35. GORM for MongoDB
    • Geospacial querying
    • GeoJSON models
    • Full text search
    • Schemaless domain models
    • Projections via MongoDB
    aggregation
    • Stateless and Stateful modes
    35

    View Slide

  36. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Designer friendly views with

    Groovy Server Pages (GSP)
    Step 6: GSP

    View Slide

  37. Groovy Server Pages (GSP)
    • View rendering engine from Grails
    • Tag libraries
    • XSS / Double encoding prevention
    • Layouts, templates and views
    37

    View Slide

  38. GSP vs. Groovy Templates
    • GSP uses standard XML/HTML markup
    • More designer friendly
    • Mature, used in Grails for years
    • Development and precompiled modes
    • XSS prevention
    • Can use existing JSP or GSP tag
    libraries
    38

    View Slide

  39. GSP in Spring Boot
    • Add grails-gsp-spring-boot as a dependency
    !
    !
    !
    !
    • Then add GSP templates with file extension .gsp to src/
    main/resources/templates
    39
    compile  "org.grails:grails-­‐gsp-­‐spring-­‐
    boot:1.0.0.RC1"

    View Slide

  40. © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
    Demo: GSP in Spring Boot

    View Slide

  41. GSP
    • Easy definition of tag libraries in Groovy code
    41
    @TagLib  
    class  FormatTagLib  {  
      def  dateFormat  =  {  attrs,  body  -­‐>  
        out  <<    
                 new  SimpleDateFormat(attrs.format)

                       .format(attrs.date)  
      }  
    }

    View Slide

  42. GSP
    • Elegant markup based views
    42
         
     
          Number  ${num}  
     
    !
    ${g.dateFormat(format:'dd-­‐MM-­‐yyyy',  
    date:new  Date())}

    View Slide

  43. !
    Q & A

    View Slide

  44. Stay Connected.
    !
    !
    !
    !
    Web: grails.org
    StackOverflow: http://stackoverflow.com/tags/grails
    Twitter: http://twitter.com/grailsframework
    LinkedIn: http://linkedin.com/groups/Grails-User-Group-39757
    Google+: https://plus.google.com/communities/
    109558563916416343008

    View Slide