spring northwest usergroup grails presentation

Post on 01-Sep-2014

1.937 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

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

Grails Presentation

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

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

– Spring IoC– Spring MVC– Hibernate– Sitemesh

● Uses groovy

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

A Java Class

And A Groovy Example

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

scripts● We can make this groovier

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

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

Grails

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

built on proven technologies you already know

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

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

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

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'

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

service com.demo.TwitterService'

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'

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

Complete● We now have a basic web application displaying results

from the twitter api

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

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

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

Spring AOP to cache the search results

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

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

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

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

Trivial Caching (cont)

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

based on method name / arguments

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

Points not Discussed● Scaffolding

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

● GORM

Questions

top related