Using Groovy Based Spring Configuration
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.
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.
One thought on “Using Groovy Based Spring Configuration”