Spring Groovy config

Spring 4.0.0 has introduced new way to define beans inside your application.
And now you can choice between:

  • XML config
  • Annotation config
  • Java config
  • Groovy config

Annotation config

You can use annotation config to forget about spring at all.
If you developing small or medium size application annotated base spring will serve all dependencies for you
and asks nothing in return. Spring will transparently creates all beans and inject it into together.
But if you want to use multiple profiles or custom bean definition it’s not a good choice.
The easiest way to use spring allows you only to use basic features.

Java config

Java config instead allows you to control everything what’s going on in your app.
You are able to catch any bean at any initialization state because you’re doing initialization by your own.
The powerful thing about java configuration in spring is independency.
You produce clean code without any sign of spring.
You’re able to create tons of ways to focus your app on environment.
But it’s very hard to maintain,
if you want to change something you need to be familiar with all initialization processes.
Of course it worths it if you need that level of countrol but usally you don’t.

XML config

XML config combines power from java config and simplicity of annotation config.
You are able to manage environment, create different scopes, define services if your own specific DSL.
But XML do it in the hard way.

Groovy config

The groovy config is a big brother of XML config. He has been cured of all xml problems.

  • It’s declarative but you can debug what’s going on inside
  • It’s as powerful as XML but very neat and brief
  • It’s a code DSL so you can invoke anything in between

Let’s take a look what you can do with Groovy in your spring app.

Define a bean
import spring.groovy.service.CatShopServiceImpl
import spring.groovy.service.CatShopServiceMockImpl

beans {

    catShopServiceMock(CatShopServiceMockImpl)

    int amountOfCats = 5
    catShopService(CatShopServiceImpl, amountOfCats)        

}

You can define a bean specifying it name as a method name and class as a 1st parameter.
To add constructor args just specify it after class.

Set bean props
import spring.groovy.service.CatShopServiceImpl

beans {

    int amountOfCats = 5

    catShopService(CatShopServiceImpl, amountOfCats) {
        listener = { CatShopServiceListener cx ->
            prefix = "Buy cats "
        }
      }

}

Nested bean, constant, bean reference, you can use anything as a bean property here.

Define custom DSL
beans {

    xmlns context:"http://www.springframework.org/schema/context"
    context.'component-scan'('base-package': "spring.groovy.scan")

}

Just define the namespace from XSD.

Invoking Java
beans {

  shopService(ShopService) {
      catShopService = ref('catShopService')
      if (Starter.env == "MOCK") {
          catShopService = ref('catShopServiceMock')
      }
  }

}

You can invoke anything from the groovy script. In example I’m calling the static env method in Starter class.

The whole project on Github.

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax