Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Spring Northwest Usergroup Grails Presentation


Published on

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.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Spring Northwest Usergroup Grails Presentation

  1. 1. Grails Presentation<br />Adam Evans<br />(<br />
  2. 2. What is Grails?<br />Rapid development framework<br />Convention over configuration<br />Built on proven technologies<br />Spring IoC<br />Spring MVC<br />Hibernate<br />Sitemesh<br />Uses groovy<br />
  3. 3. What is Groovy?<br />A scripting language which runs on the JVM<br />Groovy Objects extend java.lang.Object<br />Seamless Java integration<br />Designed for the Java developer<br />Linear learning curve<br />Familiar syntax<br />
  4. 4. A Java Class<br />
  5. 5. And A Groovy Example<br />
  6. 6. What&apos;s The Difference<br />NONE!<br />Groovy lets you use Java code in your groovy classes / scripts<br />We can make this groovier<br />
  7. 7. A Groovy Example V2<br />No constructor – groovy allows you to pass in a map to set variables<br />Closures (the each function)<br />Null safe operations &apos;?&apos;<br />Extends standard JDK classes objects (the each function)<br />Typing optional – def keyword<br />
  8. 8. Groovy<br />Allows you to use Java + Static typing when necessary<br />Adds additional functionality to standard JDK classes<br />Closures<br />MOP Meta Object Protocol<br />Java like syntax<br />Linear learning curve, you can start with Java and re-factor to make it groovier<br />
  9. 9. Grails<br />Uses best practices with the power of Groovy to make a rapid development environment built on proven technologies you already know<br />
  10. 10. Grails Cont<br />Provides a view layer (GSP&apos;s + Sitemesh)<br />Service layer (Service spring managed beans)<br />Repository (GORM, Grails object relational mapping...hibernate abstraction)<br />Unit / integration tests a key feature<br />The grails app structure<br />
  11. 11. Grails Plugins<br />Provide a full slice of the application<br />Views<br />Controllers<br />Domain objects<br />Sessions<br />Scripts<br />Allow to make a application modular, great for teams working on feature sets and to reuse ability <br />
  12. 12. Demo<br />Lets take a look at how easy it is to create a grails application and integrate spring libraries<br />We&apos;ll create a basic application to search twitter using the api, cache and display the results<br />
  13. 13. Demo (contd)<br />Create the application, type &apos;grails create-app twitter-search&apos; from the command line<br />We now have a fully functional web application, this can be run using &apos;grails run-app&apos;<br />
  14. 14. Demo (contd)<br />Create a service bean to query twitter. &apos;grails create-service com.demo.TwitterService&apos;<br />
  15. 15. Demo (contd)<br />Create a controller to query the service &apos;grails create-controller com.demo.TwitterSearch&apos;<br />Notes:<br /> 1) Spring injects the TwitterService class based on the variable name<br /> 2) Controller actions are defined as groovy closures. We return<br />a map to the view. By convention the view will be under <br />&apos;grails-app/views/${controller}/${action}‘ Iefor the above:<br /> &apos;grails-app/views/twitterSearch/search.gsp&apos;<br />
  16. 16. Demo (cont)<br />Create the view &apos;grails-app/views/twitterSearch/seach.gsp&apos;<br />Note: the layout meta tag, this tells sitemesh to decorate the page using the<br /> &apos;grails-app/layouts/main.gsp&apos; template<br />
  17. 17. Complete<br />We now have a basic web application displaying results from the twitter api<br />
  18. 18. Demo (cont)<br />Run &apos;grails-stats&apos;<br />18 lines of code !<br />
  19. 19. Integrating with Spring<br />We&apos;ve not interacted with Spring directly so far<br />Grails has taken over in the background configuring the view resolver, url mappings / controllers for Spring MVN, injecting session beans<br />
  20. 20. Spring In Grails<br />We&apos;ll look at making further use of Spring in Grails by using Spring AOP to cache the search results<br />
  21. 21. Spring AOP In Grails<br />1) We place the required lib directory<br />aspectjrt.jar (v1.5+)<br /> aspectjweaver.jar (v1.5+)<br />2) Create the spring config xml file &apos;grails-app/conf/resources.xml&apos;<br />Note: As of grails 1.2 the component-scan will not be needed as grails supports the<br />@Component annotation by default<br />
  22. 22. Create a Annotation<br />We create a Cacheable annotation under src/java.<br />
  23. 23. We create a Aspect<br />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 <br />we return the value<br />
  24. 24. Trivial Caching<br />As we apply &apos;around&apos; advice to our method it is trivial to cache.<br />Check if we have a cached value<br />If not execute method and store result<br />else load cached item<br />Return result<br />For this example grails comes with the oscache libs already so I&apos;ll use that for the caching<br />
  25. 25. Trivial Caching (cont)<br />
  26. 26. Trivial Caching (cont)<br />Below is the function we use to generate the cache key based on method name / arguments<br />
  27. 27. Conclusion<br />We&apos;ve seen how Grails integrates with<br />Java<br />Spring<br />The rapidness of creating a application<br />If you have a spring application already you can import your spring beans into Grails<br />Boilerplate code has gone, no more days lost setting up new projects writing masses of XML<br />
  28. 28. Points not Discussed<br />Scaffolding<br />Grails attempts to create basic views of your domain to get you started<br />GORM<br />
  29. 29. Questions<br />