John
John

Reputation: 77

Grails: Inject Service into the Command Object

I have a command class which needs to call a service.

import org.codehaus.groovy.grails.commons.ApplicationHolder as AH

class FilterVisitCommand {

    def accessRightsService = AH.application.mainContext.accessRightsService
    def customerService = AH.application.mainContext.customerService
...
}

This kind service definition via application holder is working however is depracated. Is there another way to let the service be injected? Only "def accessRightsService" does not work for the command class.

Upvotes: 5

Views: 3502

Answers (2)

doelleri
doelleri

Reputation: 19702

If you are injecting a service into a command object for validation, you may need to reference the service via the command object.

class FilterVisitCommand {

    def accessRightsService

    static constraints = {
        foo(validator: { foo, cmd ->
            cmd.accessRightsService.bar()
        })
    }
}

Upvotes: 15

Ruben
Ruben

Reputation: 9120

The Grails rich domain plugin enables service injection, validation, ... for regular Groovy classes.

Upvotes: 0

Related Questions