Yesterday I have attended an XTR (Xebia Tech Rally) on Groovy and Grails by Sunil & Nancy. Here I want to share my experience and learning with you all:
Groovy is a dynamic language. I wont go in much details about dynamic language but for now you can just understand like dynamic language allows you to change the behavior of your objects at run time. In Groovy every operation is executed through reflection (reflection is a programming technique of re-engineering, that means they can observe and modify themselves). I think this gives Groovy the ability to be a dynamic language (but makes it a bit slow too). There is a lot of other concepts which are there in Java but enhanced in Groovy. For example in Groovy everything is Object, even primitives are also converted in Objects, dynamic type detection etc. Groovy code is compiled in byte code and executed in JVM.
Groovy also allows you to do meta programming:
Meta programming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at runtime that would otherwise be done at compile time.
Groovy provides this feature through MetaClass interface. Have a look at the below example:
class Person {
String name
public somefunction(){
// do something
}
}
Person.metaClass.greet = {
“Hello, I’m $name”
}
def duke = new Person(name:’Duke’)?
assert duke.greet() == “Hello, I’m Duke”
You can see, when the class person is defined and compiled the method greet was not there. But we could inject method ‘greet’ to Person at run time.Similarly we can use Expando Meta Class which is an Groovy feature to inject methods to Classes & Objects (yes to objects also
) dynamically. See the code below:
class Person {
// some properties
// some methods
}
— End of class—
def emc = new ExpandoMetaClass(Person)
emc.sing = { ->
‘Hello Guest…’
}
emc.initialize()
def jack = new Person()
def paul = new Person()
jack.metaClass = emc
println jack.sing()
try
{
paul.sing()
}
catch(ex)
{
println ex
}
You can see method sing() has been added to object jack.
Grails is a Open Source full stack Web Application Development Framework based on MVC Pattern which run on JVM. Grails is basically inspired on Ruby on Rails framework. Below is the core foundation members of Grails framework:
* Spring: DI, IoC, Spring MVC, transactions…
* Hibernate: ORM
* Groovy: Super Glue
* Quartz: Job Scheduling
* Sitemesh: Page Layout & Composition
* Jetty: Fast Development
* Java Technology
In general Grails is like any other MVC framework but you have some advantages over other language’s framework:
* It uses existing Java libraries, so small learning curve for developers
* GORM (ORM based on Hibernate)
* Java like syntax
* Inbuilt Jetty server for faster development
* In memory HSQLDB for faster development
* Deployable on any J2EE container (creates war files upon project build)
* Inbuilt template system etc.
I strongly believe that Groovy and Grails will go one step forward from any other dynamic language or framework, as for Java developer there will be a very tiny learning curve. It provides addition features & powers to Java developers.
