11 Oct 2010

Using Spring DI in your Gaelyk projects

Since I started using Gaelyk, one of the features I missed most (coming from a Grails background) is Spring's dependency injection. Until recently I didn't even know if it was possible to use Spring in Google App Engine, so I decided to do a little investigation on the subject, and found out that it's very easy indeed.

Here's a little tutorial on how to configure Spring in your Gaelyk project. I'm assuming you have basic knowledge of Spring, Gaelyk and Maven.

First, let's create a Gaelyk project. The easiest way is using the excelent maven-gaelyk archetype:
mvn archetype:generate -DarchetypeGroupId=org.codeconsole -DarchetypeArtifactId=gaelyk-archetype -DarchetypeVersion=0.5.5 -DarchetypeRepository=http://maven-gaelyk.googlecode.com/svn/repository/ -DartifactId=gaelyk-spring -DgroupId=com.deluan.gaelyk -DgaeApplicationName=gaelyk-spring
Now open the project in your favorite IDE, so we can edit the configuration files. First, add the Spring dependency to your pom.xml:
Next we need to configure Spring's ContextLoaderListener in web.xml:
As you can see from above, we configured Spring to load all context configuration files under the directory WEB-INF/spring.

With these configurations in place, your project is already Spring enabled. Now we need a easy way to access the Spring's Application Context. One good way to do this is using a singleton that implements the ApplicationContextAware interface. To keep this post as small as possible, I borrowed an implementation from this blog post, where you can learn more about the details. Create the directory src/main/groovy and put the following SpringApplicationContext singleton there (in the correct package):
Configure the singleton in your spring context:
As you can see, I also declared some SimpleDateFormat instances as beans to be used in our examples.

Now everything is configured and ready to be used. Let's se how we can obtain a spring bean inside a Groovlet. Create the file WEB-INF/groovy/index.groovy with the following content

Now run your application with the command `mvn gae:run`, point your browser to http://localhost:8080/index.groovy and you should see something like this:
Index1

Well, that's it! Nothing so much different from what you are used to do in a normal Web application, right? But remember: Gaelyk is NOT your normal Web framework so let's spice things a little bit.

The solution for looking up beans depicted above is a bit cumbersome.  Let's use Gaelyk's plugin system to make things a little more "groovy". Using the plugin descriptor bellow, we can provide shortcuts to our SpringApplicationContext's methods, getContext() and getBean(). Save it in the file WEB-INF/plugins/spring.groovy:
Before you can use these shortcuts, you need to tell Gaelyk about your descriptor by "installing" it in your project. Save the code bellow in the file WEB-INF/plugins.groovy:
Now you can use the shortcuts in your Groovlets this way:
Cool, isn't it? A note on the `autowire` binding: It creates bindings "automagically" for each bean you passed as a parameter, as if the beans were declared in your Groovlet.

You can download the sample project used in this post from GitHub: http://github.com/deluan/gaelyk-spring. You can follow each commit to see exactly what was changed in each step of this tutorial.

If you have any suggestion or question, please leave a comment.

UPDATE: I've refactored the `autowire`method into a Category, so now it's not necessary to pass `this` as the first parameter.  The new version is available at GitHub
30 Sep 2010

How to use an external log4j.properties in you Grails project

In a recent Grails project, I had to follow some corporate guidelines regarding application deployment, and one of those were that the log4j configuration for the application must be externalized and in the properties format.

I searched for a Grails plugin that could help me on this, with no luck. Then I remembered that a Grails application is just a Spring application in disguise, so I looked for the Spring way to do this.

There are at least two ways to do this using Spring: Log4jConfigListener and Log4jConfigurer. I choose the later because the former assumes an expanded WAR file, which was not my case.

Here’s the recipe I came up with:

  • Configure a Log4jConfigurer Spring bean in your grails-app/conf/resources.groovy: (see here for a resources.xml version)

  • Install the templates in your project with grails install-templates, so you can change some files used for Grails' code generation. The one we are interested is the web.xml template

  • Comment out the Grails' Log4jConfigListener from the src/templates/war/web.xml template:

  • You can (and should) remove the log4j configuration block from your Config.groovy

  • That’s it!

This was tested with Grails 1.3.3, deploying to an Oracle WebLogic 10.3.0 container.