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.
2. What is Grails? Rapid development framework Convention over configuration Built on proven technologies Spring IoC Spring MVC Hibernate Sitemesh Uses groovy
3. 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
6. What's The Difference NONE! Groovy lets you use Java code in your groovy classes / scripts We can make this groovier
7. 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
8. 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
9. Grails Uses best practices with the power of Groovy to make a rapid development environment built on proven technologies you already know
10. 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
11. 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
12. 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
13. 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'
14. Demo (contd) Create a service bean to query twitter. 'grails create-service com.demo.TwitterService'
15. 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}‘ Iefor the above: 'grails-app/views/twitterSearch/search.gsp'
16. 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
17. Complete We now have a basic web application displaying results from the twitter api
19. 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
20. Spring In Grails We'll look at making further use of Spring in Grails by using Spring AOP to cache the search results
21. 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
23. 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
24. 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
26. Trivial Caching (cont) Below is the function we use to generate the cache key based on method name / arguments
27. 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
28. Points not Discussed Scaffolding Grails attempts to create basic views of your domain to get you started GORM