Hi all, my name is Hubert A. Klein Ikkink. Not a very common name, right? To make things easier I just picked the first letters of my firstname and surname and came up with haki. So there you have it, now I am also known as Mr. Haki or mrhaki for short. You can read more blog postings at www.mrhaki.com. I am a passionate Groovy and Java developer based in Tilburg, The Netherlands. My goal is to write clean, elegant, user-centered and high quality software. You can find me on Google+ and Twitter. Hubert is a DZone MVB and is not an employee of DZone and has posted 151 posts at DZone. You can read more from them at their website. View Full User Profile

Grails Goodness: Use Constructor Argument Based Dependency Injection with resources.groovy

03.18.2013
| 4784 views |
  • submit to reddit

We can define extra Spring beans for our Grails application in grails-app/conf/spring/resources.groovy using a DSL. For example we want to use third-party classes as Spring components. Then we can define the bean in resources.groovy and use dependency injection to use it in Grails controllers, services or other classes. If the class is defined so dependencies need to be injected via constructor arguments we must use a special DSL syntax. Normally we define a bean using the following syntax: beanName(BeanClass). To pass constructor arguments we must add those as method arguments after BeanClass. For example if the constructor has a single argument of type String we can use the following syntax: beanName(BeanClass, '42')

We have an alternative way and that is via a closure we can use for the bean definition. The closure will have one argument which is a BeanDefinition object. The BeanDefinition object has a constructorArgs property that takes a list of constructor arguments. So our previous example would become: beanName(BeanClass) { it.constructorArgs = ['42'] }.

Let's see how this works with an example. First we create a simple Grails service, this service will be injected into a Groovy class we will configure via constructor arguments.

// File: grails-app/services/com/mrhaki/grails/spring/LanguageService.groovy
package com.mrhaki.grails.spring

class LanguageService {

    List<String> findAllLanguages() {
        ['Groovy', 'Java', 'Scala', 'Clojure']
    }

}

Next we create a Groovy class LanguageProvider. This class will use the Grails service and a search pattern to find a language, but those are set via the constructor arguments:

// File: src/groovy/com/mrhaki/grails/spring/LanguageProvider.groovy
package com.mrhaki.spring

class LanguageProvider {

    final def languageService
    final String searchPattern

    LanguageProvider(final def languageService, final String searchPattern) {
        this.languageService = languageService
        this.searchPattern = searchPattern
    }

    List<String> getGr8Languages() {
        final List<String> languages = languageService.findAllLanguages()
        final List<String> gr8Languages = languages.findAll { it =~ searchPattern }
        gr8Languages
    }
}

To configure the LanguageProvider class as Spring bean we add the bean definition to resources.groovy:

// File: grails-app/conf/spring/resources.groovy
import com.mrhaki.spring.LanguageProvider

beans = {

    // Pass constructor arguments: ref('languageService') and '^Gr.*'
    // to LanguageProvider.
    languageProvider(LanguageProvider, ref('languageService'), '^Gr.*')

}

Or we can use the BeanDefinition that is passed to the closure:

// File: grails-app/conf/spring/resources.groovy
import com.mrhaki.spring.LanguageProvider

beans = {

    languageProvider(LanguageProvider) { beanDefinition ->
        // Pass constructor arguments: ref('languageService') and '^Gr.*'
        // to LanguageProvider.
        beanDefinition.constructorArgs = [ref('languageService'), '^Gr.*']
    }
}

Grails 2.2.1 is used to write this blog post.


 

Published at DZone with permission of Hubert Klein Ikkink, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)