Summary
A pair of short articles explores the extent of Groovy's integration with Java enterprise APIs: Guillaume Laforge reports on using Groovy with the recently released Guice IoC framework, and Romain Guy shows how to use the Java Persistence API from Groovy.
Advertisement
Since its 1.0 release late last year, Groovy has been steadily gaining momentum, as recent articles and blog posts indicate. One of Groovy's main attractions as a scripting language is that it works easily with the wealth of Java APIs in use in enterprises. A recent pair of short articles demonstrate how Groovy works with two enterprise-oriented Java frameworks: The recently released Guice IoC framework, and the Java Persistence API.
In Guicy: a Groovy Guice?, Guillaume Laforge, a leader of the Groovy project, notes that Groovy's support for Java annotations is one of the main enablers for Groovy scripts to call into Java enterprise APIs:
I'm sure [Groovy's annotation support] will propel Groovy as the de facto enterprise scripting solution leveraging the wealth of frameworks and libraries using annotations.
Guice, an IoC framework released by Bob Lee under the Apache 2.0 license, makes extensive use of annotations to provide dependency injection for enterprise applications. Guice is positioned as an XML-less way to accomplish in IoC what Spring does with XML configuration files. Laforge provides a complete example of using Guice from a Groovy script, noting that:
I'm not sure I'd use Guice for a customer project anytime soon, but for small projects where I want a xml-free DI framework, that might do! However, I might be tempted to use Grails' Spring bean builder instead, since it's a pretty cool way to avoid the usual XML-hell when working with Spring.
I was recently looking into using JPA (Java Persistence API [or Achitecture]) in desktop applications to persist the UI’s state. After all, that’s what JSR 296’s reference implementation relies on to save the frame’s size and position... What struck me ... is how clean and easy the code looks. Yet, I wanted something even easier and cleaner so I tried to use JPA/TopLink with Groovy. I encountered a few issues but ... I finally have something that works.
The very first step to use JPA with Groovy is to write your entity bean that you will persist to the database and later retrieve.... Because we are using Groovy, the getters, setters and constructors are automatically generated for us.
Having resolved a few class loading conflicts between Groovy and the Oracle TopLink implementation used in the example, Guy shows how just a few lines of Groovy code that calls into the persistence API can facilitate sophisticated data access for an application.
If you've already used Groovy in your projects, what role did Groovy play in your enterprise architecture? And if you're just thinking about using it, where do you think Groovy would fit best in a Java enterprise application?
I've used Groovy primarily to write utility scripts. It's amazing what you can do with so little code. However, I haven't tried anything like what Romain or Guillaume have done. Now that I know you can integrate Guice and JPA functionality into Groovy (not that there was any reason to believe that you couldn't), I'll have to give it a shot.