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
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
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
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
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
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
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
Groovy Spring Controllers • Just write your controller in Groovy 20 @RestController
class
ExampleController
{
@RequestMapping("/")
String
hello()
{
"Hello
World!"
}
}
Groovy Spring Controllers • Add @CompileStatic if you want type checking 21 @RestController
@CompileStatic
class
ExampleController
{
@RequestMapping("/")
String
hello()
{
"Hello
World!"
}
}
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
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"
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"
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"
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
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"
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)
}
}