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.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]