groovy and grails - chariot...

40
Grails Seminar 11/12/09 Groovy And Grails An Overview

Upload: dinhque

Post on 01-Dec-2018

255 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Grails Seminar11/12/09

Groovy And GrailsAn Overview

Page 2: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Groovy

Page 3: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

What Is Groovy?

• Groovy...

‣ Is A Dynamic Language For The Java Virtual Machine (JVM)

‣ Takes inspiration from Smalltalk, Python and Ruby (etc...)

‣ Integrates with the Java languageand platform at every level

Page 4: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Sugar In Your Java

• Groovy is Java-like‣ Easy to learn for a Java developer

flat learning curve

‣ Simpler than Java for beginners and subject matter experts

• Seamless integration with Java‣ You can mix Groovy and Java objects together

- Groovy class extending Java class implementing Groovy interface, and vice versa…

‣ Same strings, regex, APIs, OO, threads, security

Page 5: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Groovy Classclass GroovyPerson { // dynamically typed property def age // statically typed property String name def printName() { println name } static void main(String[] args) { def person = new GroovyPerson(age: 8, name: 'Jake') person.printName() }}

Page 6: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

• Groovy Beans / POGOs

• Similar To POJOs

‣ ...but groovier

• Eliminates Boilerplate Code

• Simple To Achieve JavaBean Compliance

Groovy Beans

Page 7: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

POJO

public class Person { private String firstName; private String lastName; public Person() { } public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }

Page 8: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

POJO public void setLastName(String lastName) { this.lastName = lastName; }

public String getLastName() { return lastName; } public void setFirstName(String firstName) { this.firstName = firstName; }

public String getFirstName() { return firstName; }}

Page 9: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

• Modern Java IDEs Generate Most Of That Code

‣ developer declares fields

‣ IDE generates constructors

‣ IDE generates getters/setters

If the IDE can generate all of that code, why can't the compiler or the runtime?

POJO

Page 10: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

• Groovy Beans Eliminate All Of The Boilerplate Code

• No Need To Write Getters/Setters

• Seldom Need To Write Constructors

Groovy Beans

Page 11: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Groovy Beans

class BaseballTeam { def cityName def teamName}

def myTeam = new BaseballTeam(teamName: 'Cardinals', cityName: 'St. Louis') println myTeam.teamNameprintln myTeam.cityName

Page 12: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

• Property Access Looks Like Field

Groovy Beans

def myTeam = new BaseballTeam() // myTeam.setTeamName('Cardinals') myTeam.teamName = 'Cardinals'

// myTeam.setCityName('St. Louis')myTeam.cityName = 'St. Louis'

// println myTeam.getTeamName()println myTeam.teamName

Page 13: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Print Independence Day

// PrintIndependenceDay.javaimport java.util.Calendar;import java.util.Date;

public class PrintIndependenceDay { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.MONTH, Calendar.JULY); calendar.set(Calendar.DATE, 4); calendar.set(Calendar.YEAR, 1776); Date time = calendar.getTime(); System.out.println(time); }}

Page 14: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Print Independence Day

// PrintIndependenceDay.groovyimport java.util.Calendar;import java.util.Date;

public class PrintIndependenceDay { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.MONTH, Calendar.JULY); calendar.set(Calendar.DATE, 4); calendar.set(Calendar.YEAR, 1776); Date time = calendar.getTime(); System.out.println(time); }}

Page 15: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

No Utility Imports...

// PrintIndependenceDay.groovy

public class PrintIndependenceDay { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.MONTH, Calendar.JULY); calendar.set(Calendar.DATE, 4); calendar.set(Calendar.YEAR, 1776); Date time = calendar.getTime(); System.out.println(time); }}

Page 16: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

No Semicolons...

// PrintIndependenceDay.groovypublic class PrintIndependenceDay { public static void main(String[] args) { Calendar calendar = Calendar.getInstance() calendar.clear() calendar.set(Calendar.MONTH, Calendar.JULY) calendar.set(Calendar.DATE, 4) calendar.set(Calendar.YEAR, 1776) Date time = calendar.getTime() System.out.println(time) }}

Page 17: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

No Getters...

// PrintIndependenceDay.groovypublic class PrintIndependenceDay { public static void main(String[] args) { Calendar calendar = Calendar.instance calendar.clear() calendar.set(Calendar.MONTH, Calendar.JULY) calendar.set(Calendar.DATE, 4) calendar.set(Calendar.YEAR, 1776) Date time = calendar.time System.out.println(time) }}

Page 18: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

No Static Typing...

// PrintIndependenceDay.groovypublic class PrintIndependenceDay { public static void main(String[] args) { def calendar = Calendar.instance calendar.clear() calendar.set(Calendar.MONTH, Calendar.JULY) calendar.set(Calendar.DATE, 4) calendar.set(Calendar.YEAR, 1776) def time = calendar.time System.out.println(time) }}

Page 19: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

No System.out.blah.blah...

// PrintIndependenceDay.groovypublic class PrintIndependenceDay { public static void main(String[] args) { def calendar = Calendar.instance calendar.clear() calendar.set(Calendar.MONTH, Calendar.JULY) calendar.set(Calendar.DATE, 4) calendar.set(Calendar.YEAR, 1776) def time = calendar.time println(time) }}

Page 20: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

No Class...

// PrintIndependenceDay.groovy

def calendar = Calendar.instancecalendar.clear()calendar.set(Calendar.MONTH, Calendar.JULY)calendar.set(Calendar.DATE, 4)calendar.set(Calendar.YEAR, 1776)

def time = calendar.time

println(time)

Page 21: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Optional Parens...

// PrintIndependenceDay.groovy

def calendar = Calendar.instancecalendar.clear()calendar.set Calendar.MONTH, Calendar.JULYcalendar.set Calendar.DATE, 4calendar.set Calendar.YEAR, 1776

def time = calendar.time

println time

Page 22: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Lets Go Meta...

// PrintIndependenceDay.groovy

def calendar = Calendar.instancecalendar.with { clear() set MONTH, JULY set DATE, 4 set YEAR, 1776 println time}

Page 23: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Lets Compare...

// PrintIndependenceDay.groovy

def calendar = Calendar.instancecalendar.with { clear() set MONTH, JULY set DATE, 4 set YEAR, 1776 println time}

// PrintIndependenceDay.javaimport java.util.Calendar;import java.util.Date;

public class PrintIndependenceDay { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.MONTH, Calendar.JULY); calendar.set(Calendar.DATE, 4); calendar.set(Calendar.YEAR, 1776); Date time = calendar.getTime(); System.out.println(time); }}

Page 24: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Grails

Page 25: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

What Is Grails?

• A Web platform that implements the full stack from build system down to ORM layer

• Leverages existing technologies like Spring, Hibernate, Quartz etc. avoiding re-inventing the wheel

• Features and extensible plug-in system and an environment for runtime configuration built on Spring

Page 26: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Best Of Breed

• Spring

• Hibernate

• Groovy

• Quartz

• Sitemesh

• Tomcat

• Ant

Spring

Hibernate

QuartzJava

Sitemesh

Groovy

Page 27: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

The Grails Stack

The Java Virtual Machine

Groovy

Java Enterprise

Edition (JEE)Spring Hibernate SiteMesh

Grails

The Java LanguageThe Java Development

Kit (JDK)

Page 28: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

More than Just a Web Framework

• Grails delivers more than your regular web framework - a full stack.

• Grails aims to ease development of every tier and features

‣ An integrated Groovy build system

‣ An incredibly simple ORM layer built on Hibernate

‣ An amazing Groovy-based view technology called GSP

Page 29: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Sensible Defaults

• Quickly get started

‣ An in-memory HSQLDB

‣ A built-in Tomcat servlet container

‣ The ability to generate a WAR file out of the box

‣ A built-in interactive console and shell

‣ An ant build.xml file with useful targets like war, test etc.

‣ IDE project files

Page 30: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Querying

• GORM supports a number of ways to query including:

‣ Dynamic Finders

‣ Criteria

‣ Query-by-example

‣ HQL

Put image here!

Page 31: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Dynamic Finders

• Automatically translate the properties of the class into "method expressions" - at runtime!

• Uses the Hibernate Criteria API underneath

• Rich and expressive way to query

Page 32: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Dynamic Finders

def all = Bookmark.list()

// user like expressionsdef grailsBookmarks = Bookmark.findAllByTitleLike("%Grails%")

// query between two valuesdef now = new Date()def lastWeeks = Bookmark.findByCreatedDateBetween(now-7, now)

// query associationsdef bookmark = Bookmark.get(34)def comments = Comment.findAllByBookmark(bookmark)

Page 33: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Querying With Criteria

// returns first 10 users who have an active // account that has been created in the last // 30 days and that have Grails-like // bookmarks created in the last 7 daysdef now = new Date()def users = Bookmark.withCriteria { comments { like("text","%Grails%") between("dateCreated", now-7, now) } between("dateCreated",now-30, now) maxResults(10)}

Page 34: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Querying with HQL

• If all else fails, there is always HQL!:

// Query for Bookmark instancesdef bookmarks = Bookmark.findAll("from Bookmark b where b.title like ?", ["%Grails%"] )

// select only the Bookmark titlesdef titles = Bookmark.executeQuery( "select b.title from Bookmark b where b.title like ?", ["%Groovy%"] )

Page 35: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

The Grails Plugin System

Page 36: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

The Background• Grails is designed to wire

together different libraries and make them easy to use

• In this sense it can be seen as a "platform for runtime configuration"

• De-coupling those components was hard without a well defined system

Page 37: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

The Extension Points

• The Build System

• Spring Application Context

• Dynamic method registration

• Auto Reloading

• Container Config

Page 38: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

What can a Plug-in do?

• Add new methods, constructors, properties etc. to any class at runtime

• Perform runtime Spring configuration

• Modify web.xml on the fly

• Add new controllers, tag libraries etc.

Page 39: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

A Look Ahead

• 1.2 Is Just Around The Corner

• Performance, Stability, New Features Etc...

• Focus On Plugins

• Cloud Foundry Etc...

Page 40: Groovy And Grails - Chariot Solutionschariotsolutions.com/.../archive/449/groovy-grails-keynote.pdf · Groovy And Grails An Overview. Groovy. What Is Groovy?

Q & A