Tuesday, December 24, 2013

Spring 4 with Groovy

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 the Java.

Then, is 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

No comments:

Post a Comment