Jan 12, 2016

Using Groovy Based Spring Configuration

Introduction

The Spring framework began with using XML as the only means for bean configuration. As Spring grew and more features were added to the framework, XML configuration got to be very verbose and it took a lot of XML to do simple things. Java configuration was added to solve this problem as beans could be created programmatically and method calls could be made. This was a big improvement from the overly verbose XML configuration that has been the staple of Spring since its inception. In this post, I would like to expand on this concept by using Groovy in place of Java for the annotation driven configuration as well as the Grails style of configuration added in Spring 4.

Using Groovy as Java Configuration

Since version 4, Spring added support for Groovy, so any Groovy classes can be Spring beans and Groovy can be used in place of Java for annotation driven configuration. The benefit of this is that all the features of Groovy can be used when creating a bean in Spring. Below are a couple examples of configuration in Java vs Groovy.

The first example is a Java configuration which creates a map and a list.

The code is fairly standard, but now here is the same example written in Groovy.

The code is much more concise in creating the map and list, but any of Groovy’s features can be used, such as closures, XML and JSON slupers which all make the configuration more concise. Below is an example of using Groovy’s XmlSlurper to load in an XML file and put the results in a map to be used as a Spring bean. First the XML file.

Next is the Groovy configuration class.

It goes without saying that the amount of Java code to accomplish what is above could easily be doubled or even tripled.

Using Grails Style Groovy Configuration

Another new feature of Spring 4 was the addition of Grails style Groovy configuration. This style uses Groovys configuration style to create Spring beans and is used in Grails in resources.groovy and allows the use of all the Groovy features as well as concise syntax. Below is an example.

Groovy’s configuration style is well suited for use for Spring configuration and adds the ability to programmatically add in method calls if necessary.

Adding a Groovy Configuration to web.xml

Spring version 4.1 added a way to get a Groovy configuration file added to web.xml with the addition of GroovyWebApplicationContext, the below example of web.xml shows how to configure it.

By adding a contextClass parameter with GroovyWebApplicationContext and then the Groovy application context, Spring will know how to bootstrap the application with a Groovy context file. A dependency on spring-web 4.1.x will be needed to add this class into the application.

Summary

Using Groovy for Spring configuration opens up a number of advantages, including concise syntax, nicer looking configuration files and being able to add methods if needed. If a project is using Spring 4 or 4.1 and above, it’s worth a look.

About the Author

Object Partners profile.

One thought on “Using Groovy Based Spring Configuration

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Tracking Original URL Through Authentication
If you read my other post about refreshing AWS tokens, then you probably have a use case for keeping track of the original requested resource while the user goes through authentication so you can route […]
Using Spring Beans in a Kafka Streams ExceptionHandler
There are many things to know before diving into Kafka Streams. If you haven’t already, check out these 5 things as a starting point. Bullet 2 mentions designing for exceptions. Ironically, this seems to be […]