Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.apache.groovy</groupId>
    <artifactId>groovy</artifactId>
    <version>4.0.21</version>
</dependency>

Now, let’s add the plugin:

<build>
    <plugins>
        //...
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>addSources</goal>
                        <goal>addTestSources</goal>
                        <goal>generateStubs</goal>
                        <goal>compile</goal>
                        <goal>generateTestStubs</goal>
                        <goal>compileTests</goal>
                        <goal>removeStubs</goal>
                        <goal>removeTestStubs</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Here, we use gmavenplus-plugin with all the goals.

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

3. Defining Beans

Since version 4, Spring provides 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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW