Spring Northwest Usergroup Grails Presentation

29
Grails Presentation Adam Evans (http://www.ctisn.com)

description

This is a presentation I gave at the Spring Northwest Usergroup (Manchester) 01/12/2009 . I quickly go over the basics of grails / groovy and then demonstrate using Spring AOP in grails showing how easy it is to use your existing spring beans and spring projects.

Transcript of Spring Northwest Usergroup Grails Presentation

Page 1: Spring Northwest Usergroup Grails Presentation

Grails Presentation

Adam Evans(http://www.ctisn.com)

Page 2: Spring Northwest Usergroup Grails Presentation

What is Grails?● Rapid development framework● Convention over configuration● Built on proven technologies

– Spring IoC– Spring MVC– Hibernate– Sitemesh

● Uses groovy

Page 3: Spring Northwest Usergroup Grails Presentation

What is Groovy?

● A scripting language which runs on the JVM● Groovy Objects extend java.lang.Object

– Seamless Java integration● Designed for the Java developer

– Linear learning curve– Familiar syntax

Page 4: Spring Northwest Usergroup Grails Presentation

A Java Class

Page 5: Spring Northwest Usergroup Grails Presentation

And A Groovy Example

Page 6: Spring Northwest Usergroup Grails Presentation

What's The Difference● NONE!● Groovy lets you use Java code in your groovy classes /

scripts● We can make this groovier

Page 7: Spring Northwest Usergroup Grails Presentation

A Groovy Example V2● No constructor – groovy allows you to pass in a map to set

variables● Closures (the each function)● Null safe operations '?'● Extends standard JDK classes objects (the each function)● Typing optional – def keyword

Page 8: Spring Northwest Usergroup Grails Presentation

Groovy● Allows you to use Java + Static typing when necessary● Adds additional functionality to standard JDK classes● Closures● MOP Meta Object Protocol● Java like syntax● Linear learning curve, you can start with Java and re-factor

to make it groovier

Page 9: Spring Northwest Usergroup Grails Presentation

Grails

●Uses best practices with the power of Groovy to make a rapid development environment

built on proven technologies you already know

Page 10: Spring Northwest Usergroup Grails Presentation

Grails Cont● Provides a view layer (GSP's + Sitemesh)● Service layer (Service spring managed beans)● Repository (GORM, Grails object relational

mapping...hibernate abstraction)● Unit / integration tests a key feature

The grails app structure

Page 11: Spring Northwest Usergroup Grails Presentation

Grails Plugins● Provide a full slice of the application

– Views– Controllers– Domain objects– Sessions– Scripts

● Allow to make a application modular, great for teams working on feature sets and to reuse ability

Page 12: Spring Northwest Usergroup Grails Presentation

Demo

Lets take a look at how easy it is to create a grails application and integrate spring libraries

We'll create a basic application to search twitter using the api, cache and display the

results

Page 13: Spring Northwest Usergroup Grails Presentation

Demo (contd)● Create the application, type 'grails create-app twitter-

search' from the command line

We now have a fully functional web application, this can be run using 'grails run-app'

Page 14: Spring Northwest Usergroup Grails Presentation

Demo (contd)● Create a service bean to query twitter. 'grails create-

service com.demo.TwitterService'

Page 15: Spring Northwest Usergroup Grails Presentation

Demo (contd)● Create a controller to query the service 'grails create-

controller com.demo.TwitterSearch'

Notes: 1) Spring injects the TwitterService class based on the variable name 2) Controller actions are defined as groovy closures. We return a map to the view. By convention the view will be under 'grails-app/views/${controller}/${action}‘ Ie for the above: 'grails-app/views/twitterSearch/search.gsp'

Page 16: Spring Northwest Usergroup Grails Presentation

Demo (cont)● Create the view 'grails-app/views/twitterSearch/seach.gsp'

Note: the layout meta tag, this tells sitemesh to decorate the page using the 'grails-app/layouts/main.gsp' template

Page 17: Spring Northwest Usergroup Grails Presentation

Complete● We now have a basic web application displaying results

from the twitter api

Page 18: Spring Northwest Usergroup Grails Presentation

Demo (cont)● Run 'grails-stats'● 18 lines of code !

Page 19: Spring Northwest Usergroup Grails Presentation

Integrating with Spring● We've not interacted with Spring directly so far● Grails has taken over in the background configuring the

view resolver, url mappings / controllers for Spring MVN, injecting session beans

Page 20: Spring Northwest Usergroup Grails Presentation

Spring In Grails● We'll look at making further use of Spring in Grails by using

Spring AOP to cache the search results

Page 21: Spring Northwest Usergroup Grails Presentation

Spring AOP In Grails● 1) We place the required lib directory

– aspectjrt.jar (v1.5+)– aspectjweaver.jar (v1.5+)

● 2) Create the spring config xml file 'grails-app/conf/resources.xml'

Note: As of grails 1.2 the component-scan will not be needed as grails supports the@Component annotation by default

Page 22: Spring Northwest Usergroup Grails Presentation

Create a Annotation● We create a Cacheable annotation under src/java.

Page 23: Spring Northwest Usergroup Grails Presentation

We create a Aspect

With the above, any method with the annotation @Cacheable will be handled by the aroundAdvice method. For now we just called proceed() so the method We are intercepting is executed and we return the value

Page 24: Spring Northwest Usergroup Grails Presentation

Trivial Caching● As we apply 'around' advice to our method it is trivial to

cache.– Check if we have a cached value

● If not execute method and store result– else load cached item

● Return result● For this example grails comes with the oscache libs

already so I'll use that for the caching

Page 25: Spring Northwest Usergroup Grails Presentation

Trivial Caching (cont)

Page 26: Spring Northwest Usergroup Grails Presentation

Trivial Caching (cont)● Below is the function we use to generate the cache key

based on method name / arguments

Page 27: Spring Northwest Usergroup Grails Presentation

Conclusion● We've seen how Grails integrates with

– Java– Spring

● The rapidness of creating a application● If you have a spring application already you can import

your spring beans into Grails● Boilerplate code has gone, no more days lost setting up

new projects writing masses of XML

Page 28: Spring Northwest Usergroup Grails Presentation

Points not Discussed● Scaffolding

– Grails attempts to create basic views of your domain to get you started

● GORM

Page 29: Spring Northwest Usergroup Grails Presentation

Questions