Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

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:


Now, let's add the plugin:


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:

public class JavaBeanConfig {

    public JavaPersonBean javaPerson() {
        JavaPersonBean jPerson = new JavaPersonBean();
        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:


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:


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


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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


Generic footer banner
Comments are closed on this article!