<

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

In this quick article, we’ll focus on how we can use a Groovy-based configuration in our Java Spring projects.

2. Dependencies

Before we start, we need to add the dependency to our pom.xml file. We need to also add a plugin for the sake of compiling our Groovy files.

Let’s add the dependency for Groovy first to our pom.xml file:

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.4.12</version>
</dependency>

Now, let’s add the plugin:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <compilerId>groovy-eclipse-compiler</compilerId>
        <verbose>true</verbose>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-compiler</artifactId>
            <version>2.9.2-01</version>
        </dependency>
    </dependencies>
</plugin>

Here, we use the Maven compiler to compile the project by using the Groovy-Eclipse compiler. This may vary depending on the IDE we use.

The latest versions of these libraries can be found on Maven Central.

3. Defining Beans

Since version 4, Spring provides a support for Groovy-based configurations. This means that Groovy classes can be legitimate Spring beans.

To illustrate this, we’re going to define a bean using the standard Java configuration and then we’re going to configure the same bean using Groovy. This way, we’ll be able to see the difference.

Let’s create a simple class with a few properties:

public class JavaPersonBean {
    private String firstName;
    private String lastName;

    // standard getters and setters
}

It’s important to remember about getters/setters – they’re crucial for the mechanism to work.

3.1. Java Configuration

We can configure the same bean using a Java-based configuration:

@Configuration
public class JavaBeanConfig {

    @Bean
    public JavaPersonBean javaPerson() {
        JavaPersonBean jPerson = new JavaPersonBean();
        jPerson.setFirstName("John");
        jPerson.setLastName("Doe");
        
        return jPerson;
    }
}

3.2. Groovy Configuration

Now, we can see the difference when we use Groovy to configure the previously created bean:

beans {
    javaPersonBean(JavaPersonBean) {
        firstName = 'John'
        lastName = 'Doe'
    }
}

Note that before defining beans configuration, we should import the JavaPersonBean class. Also, inside the beans block, we can define as many beans as we need.

We defined our fields as private and although Groovy makes it look like it’s accessing them directly, it’s doing it using provided getters/setters.

4. Additional Bean Settings

As with the XML and Java-based configuration, we can configure not only beans.

If we need to set an alias for our bean, we can do it easily:

registerAlias("bandsBean","bands")

If we want to define the bean’s scope:

{ 
    bean ->
        bean.scope = "prototype"
}

To add lifecycle callbacks for our bean, we can do:

{ 
    bean ->
        bean.initMethod = "someInitMethod"
        bean.destroyMethod = "someDestroyMethod"
}

We can also specify inheritance in the bean definition:

{ 
    bean->
        bean.parent="someBean"
}

Finally, if we need to import some previously defined beans from an XML configuration, we can do this using the importBeans():

importBeans("somexmlconfig.xml")

5. Conclusion

In this tutorial, we saw how we to create Spring Groovy bean configurations. We also covered setting additional properties on our beans such as their aliases, scopes, parents, methods for initialization or destruction, and how to import other XML-defined beans.

Although the examples are simple, they can be extended and used for creating any type of Spring config.

A full example code that is used in this article can be found in our GitHub project. This is a Maven project, so you should be able to import it and run it as it is.

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS