groovy & grails for spring/java developers

Post on 10-May-2015

1.351 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Presentation given at SpringOne 2GX. The original presentation is mostly demo, but these slides show some nice features of Groovy and how Grails can support the varying needs to enterprise developers.

TRANSCRIPT

Groovy & Grails for Java developers

Peter Ledbrook, Developer Advocatepledbrook@vmware.com / @pledbrook

© 2012 SpringOne 2GX. All rights reserved. Do not distribute without permission.

1

Demo

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

10

10

11

11

12

12

13

13

14

14

15

15

16

16

17

17

18

18

19

19

20

20

21

21

22

22

23

23

24

24

25

25

26

26

27

27

28

28

29

29

30

30

31

31

32

32

33

33

34

34

35

35

36

36

37

37

38

38

39

39

40

40

41

41

42

42

43

43

44

44

45

45

46

46

47

47

48

48

49

49

50

50

51

51

52

52

53

53

54

54

55

55

56

56

57

57

58

58

59

59

60

60

• Grails for Swing applications• MVC model• SwingBuilder for views

61

application(title: 'DemoConsole', pack: true, locationByPlatform: true) { panel(border: emptyBorder(6)) { borderLayout() scrollPane(constraints: CENTER) { textArea( text: bind(target: model, targetProperty: 'scriptSource'), enabled: bind {model.enabled}, columns: 40, rows: 10) } }}

http://griffon.codehaus.org/

61

Gradle

• Build tool with built-in dependency management• Conventions through plugins• Multi-project support• Full access to tasks and dependency tree• Easy to write your own tasks

– either in the build file– or via Groovy/Java classes

62

http://www.gradle.org/

62

• Parallel processing for Groovy• Actors library making full use of closures

63

@Grab(group='org.codehaus.gpars', module='gpars', version='0.11')import groovyx.gpars.GParsPool

GParsPool.withPool { def animals = ['dog', 'ant', 'cat', 'whale'] println(animals.anyParallel {it ==~ /ant/} ? 'Found an ant' : 'No ants found') println(animals.everyParallel {it.contains('a')} ? 'All animals contain a' : 'Some animals can live without an a')}

http://gpars.codehaus.org/

63

Try it out!

64

http://groovyconsole.appspot.com/

64

What is Grails?

• Rapid Web Application Development Framework– for the JVM– with first-class Java integration

• Inspired by Ruby on Rails, Django and others– Convention over Configuration– Don’t Repeat Yourself (DRY)

65

65

Grails

What is Grails?

66

Build

Web MVC GSP (Views)

GORM(Data Access)

Doc Engine

Servlet Container

Test Support

Database I18n

66

Grails

What is Grails?

67

67

What is Grails?

68

Web ControllersThe Domain Model

Business Logic

Custom View TagsViews & Layouts

Libraries (JARs)

Additional Sources

Web Resources

i18n bundles

Build Commands

Tests

68

Say bye-bye to the plumbing!

69

69

Demo

70

Enterprise requirements

71

Web App

Messaging

Legacy Databases Services

JEE

Is this a problem for Grails apps?

71

Build

72

• Remember the Grails project structure?– add in build events and...

Can’t build natively with other build tools!

Grails Build System

Ant GradleMaven

72

Dependency DSL

grails.project.dependency.resolution = { inherits "global" log "warn" repositories { grailsHome() mavenCentral() mavenRepo "http://localhost:8081/..." } ...}

73

73

Dependency DSL

grails.project.dependency.resolution = { inherits "global" log "warn" ... dependencies { runtime "mysql:mysql-connector-java:5.1.17" test "org.gmock:gmock:0.8.1" ... } plugins { compile ":spring-security-core:1.2.7" ... }}

74

74

‘Legacy’ Databases

• Grails can create a database from your domain model...• ...but what if you don’t own the database?

– DBA determines structure– Company conventions– Existing ‘legacy’ database

75

75

• No existing domain model• Schema not too far off the beaten track

76

Option 1: Custom ORM mapping

class Book { ... static mapping = { table "books" title type: "books" author column: "author_ref" }}

76

• Existing Java/JPA domain model

Option 2: JPA annotations

77

grails-app/conf/hibernate/hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE ...><hibernate-configuration> <session-factory> <mapping class="org.ex.Book"/> <mapping class="org.ex.Author"/> ... </session-factory></hibernate-configuration>

77

• You have Java model + Hibernate mapping files• Schema is way off the beaten track

78

grails-app/conf/hibernate/hibernate.cfg.xml

Option 3: Hibernate XML Mappings

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE ...><hibernate-configuration> <session-factory> <mapping resource="org.ex.Book.hbm.xml"/> <mapping resource="org.ex.Author.hbm.xml"/> ... </session-factory></hibernate-configuration>

78

constraints = { title blank: false, unique: true ...}

Constraints

Given domain class:

Then:

org.example.myapp.domain.Book

src/java/org/example/myapp/domain/BookConstraints.groovy

79

79

• GORM layer over JPA• Use your own JPA provider• Useful for cloud services that only work with JPA, not

Hibernate

80

Option 4: GORM JPA Plugin

80

Database Migration Plugin

81

Pre-production, Hibernate ‘update’ or ‘create-drop’

dbm-generate-changelogdbm-changelog-sync

Change domain model

dbm-gorm-diffdbm-update

81

Reverse Engineering Plugin

82

class Person { String name Integer age ...}

82

Grails is Spring

• Spring MVC under the hood• Grails provides many useful beans

– e.g. grailsApplication• Define your own beans!

– resources.xml/groovy– In a plugin

83

83

Example

import ...beans = { credentialMatcher(Sha1CredentialsMatcher) { storedCredentialsHexEncoded = true }

sessionFactory(ConfigurableLocalSessionFactoryBean) { dataSource = ref("dataSource") hibernateProperties = [ "hibernate.hbm2ddl.auto": "create-drop", "hibernate.show_sql": true ] }}

84

84

Summary

• Various options for integrating Grails with:– Development/build– Deployment processes

• Works with many external systems– Solid support for non-Grailsy DB schemas– Flexible messaging & web service support

85

85

More info

• w: http://grails.org/• f: http://grails.org/Mailing+Lists

• e: pledbrook@vmware.com• t: pledbrook• b: http://blog.springsource.com/author/peter-ledbrook/

86

86

Q&A

87

top related