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
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Grails Goodness: Use Constructor Argument Based Dependency Injection with resources.groovy

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

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Mar. 19, 13 · Interview
Like (0)
Save
Tweet
Share
9.13K Views

Join the DZone community and get the full member experience.

Join For Free

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.


 

Grail (web browser) Dependency injection

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Secrets Management
  • Top 12 Technical Skills Every Software Tester Must Have
  • The Data Leakage Nightmare in AI
  • Cloud-Native Application Networking

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: