DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management

Trending

  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough
  • The Real Cost of Change in Software Development
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • Constrained Decoding Models Explained: Encoders, Decoders and When to Use Them

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Spring 4 with Groovy

Spring 4 with Groovy

By 
Felipe Gutierrez
·
Dec. 26, 13 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
27.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

 Finally the wait is over, Spring 4 is here and one of the best features is that now has a fully support for Groovy. The Spring team did a fantastic job bringing the same concept from Grails into the Spring Framework.

In the past if you needed to incorporate Spring in your Groovy Applications or Scripts , normally you did something like this:


@Grab('org.springframework:spring-context:4.0.0.RELEASE')

import org.springframework.context.support.GenericApplicationContext
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class Login {

    def authorize(User user){
        if(user.credentials.username == "guest" && user.credentials.password == "guest"){
            "${user.greetings} ${user.credentials.username}"
        }else
            "You are not ${user.greetings}"
    }
}

@Component
class Credentials {
    String username = "guest"
    String password = "guest"
}

@Component
class User{
    @Autowired
    Credentials credentials
    String greetings = "Welcome"
}

def ctx = new GenericApplicationContext()
new ClassPathBeanDefinitionScanner(ctx).scan('') // scan root package for components
ctx.refresh()

def login = ctx.getBean("login")
def user  = ctx.getBean("user")
println login.authorize(user)

One of the benefits that I see is that Groovy removes all the unnecessary boiler plate from Java.

Then, if you wanted to add more flavor to your Groovy Apps using the Grail's BeanBuilder , you needed to do something like this:
@Grab(group='org.slf4j', module='slf4j-simple', version='1.7.5')
@Grab(group='org.grails', module='grails-web', version='2.3.4')

import grails.spring.BeanBuilder
import groovy.transform.ToString

class Login {

    def authorize(User user){
        if(user.credentials.username == "John" && user.credentials.password == "Doe"){
            "${user.greetings} ${user.credentials.username}"
        }else
            "You are not ${user.greetings}"
    }
}

@ToString(includeNames=true)
class Credentials{
    String username
    String password
}

@ToString(includeNames=true)
class User{
    Credentials credentials
    String greetings
}


def bb = new BeanBuilder()

bb.beans {

    login(Login)

    user(User){
        credentials = new Credentials(username:"John", password:"Doe")
        greetings = 'Welcome!!'
    }

}

def ctx = bb.createApplicationContext()

def u = ctx.getBean("user")
println u

def l = ctx.getBean("login")
println l.authorize(u)


And now with the new Spring 4 you can add all the Groovy flavor to your apps (without all the Grails Dependencies) and using the new GroovyBeanDefinitionReader from Spring 4:
@Grab('org.springframework:spring-context:4.0.0.RELEASE')

import org.springframework.context.support.GenericApplicationContext
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader

import groovy.transform.ToString

class Login {

    def authorize(User user){
        if(user.credentials.username == "John" && user.credentials.password == "Doe"){
            "${user.greetings} ${user.credentials.username}"
        }else
            "You are not ${user.greetings}"
    }
}

@ToString(includeNames=true)
class Credentials{
    String username
    String password
}

@ToString(includeNames=true)
class User{
    Credentials credentials
    String greetings
}


def ctx = new GenericApplicationContext()
def reader = new GroovyBeanDefinitionReader(ctx)

reader.beans {

    login(Login)

    user(User){
        credentials = new Credentials(username:"John", password:"Doe")
        greetings = 'Welcome!!'
    }

}

ctx.refresh()

def u = ctx.getBean("user")
println u

def l = ctx.getBean("login")
println l.authorize(u)


And that's not all, also SpringBoot  gives you even more power using Groovy:

//File: app.groovy
import org.springframework.boot.*
import org.springframework.boot.autoconfigure.*
import org.springframework.stereotype.*
import org.springframework.web.bind.annotation.*
import org.springframework.beans.factory.annotation.*
	
	
@Component
class Login {

    def authorize(User user){
        if(user.credentials.username == "John" && user.credentials.password == "Doe"){
            "${user.greetings} ${user.credentials.username}"
        }else
            "You are not ${user.greetings}"
    }
}

@Component
class Credentials{
    String username
    String password
}

@Component
class User{
    Credentials credentials = new Credentials(username:"John", password:"Doe")
    String greetings = "Welcome!!"
}


@Controller
@EnableAutoConfiguration
class SampleController {

	@Autowired
	def login
		
	@Autowired
	def user

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return login.authorize(user)
    }
}


To run the above code, install springboot  and then just execute:

$ spring run app.groovy

and then go to your browser and open http://localhost:8080  and you see:

Welcome!! John

Congratulations to the Spring Team!!
Keep grooving!!

References:
[1] https://spring.io/blog/2013/12/12/announcing-spring-framework-4-0-ga-release
[2] http://groovy.codehaus.org/Using+Spring+Factories+with+Groovy
[3] http://grails.org/doc/latest/guide/spring.html
[4] http://projects.spring.io/spring-boot/
[5] https://spring.io/guides










Spring Framework Groovy (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • ⚽ Live Match: articles/spring groovy - Info Live Streaming 2026
  • ⚽ Live Match: articles/spring groovy - Info Live Streaming 2026
  • ⚽ Live Match: articles/spring groovy - Info Live Streaming 2026

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • ⚽ Live Match: articles/spring groovy - Info Live Streaming 2026
  • ⚽ Live Match: articles/spring groovy - Info Live Streaming 2026
  • ⚽ Live Match: articles/spring groovy - Info Live Streaming 2026