![Page 1: Spring Northwest Usergroup Grails Presentation](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/1.jpg)
Grails Presentation
Adam Evans(http://www.ctisn.com)
![Page 2: Spring Northwest Usergroup Grails Presentation](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/2.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/3.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/4.jpg)
A Java Class
![Page 5: Spring Northwest Usergroup Grails Presentation](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/5.jpg)
And A Groovy Example
![Page 6: Spring Northwest Usergroup Grails Presentation](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/6.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/7.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/8.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/9.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/10.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/11.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/12.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/13.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/14.jpg)
Demo (contd)● Create a service bean to query twitter. 'grails create-
service com.demo.TwitterService'
![Page 15: Spring Northwest Usergroup Grails Presentation](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/15.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/16.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/17.jpg)
Complete● We now have a basic web application displaying results
from the twitter api
![Page 18: Spring Northwest Usergroup Grails Presentation](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/18.jpg)
Demo (cont)● Run 'grails-stats'● 18 lines of code !
![Page 19: Spring Northwest Usergroup Grails Presentation](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/19.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/20.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/21.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/22.jpg)
Create a Annotation● We create a Cacheable annotation under src/java.
![Page 23: Spring Northwest Usergroup Grails Presentation](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/23.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/24.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/25.jpg)
Trivial Caching (cont)
![Page 26: Spring Northwest Usergroup Grails Presentation](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/26.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/27.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/28.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061112/540524ba8d7f72a6768b4a96/html5/thumbnails/29.jpg)
Questions