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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring 4 with Groovy

Spring 4 with Groovy

Felipe Gutierrez user avatar by
Felipe Gutierrez
·
Dec. 26, 13 · Interview
Like (4)
Save
Tweet
Share
27.06K Views

Join the DZone community and get the full member experience.

Join For Free

 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.

Popular on DZone

  • Deploying Java Serverless Functions as AWS Lambda
  • Bye Bye, Regular Dev [Comic]
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer
  • ChatGPT Prompts for Agile Practitioners

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: