Mike Caputo
Mike Caputo

Reputation: 1156

Can't inject grailsApplication into service

I'm having an issue injecting grailsApplication into a service I created in Grails 2.1. I have built a simple test application (hoping I could figure out this issue and then port the fix over to my real application) that consists of a single controller, view, and service.

The controller looks like this:

class IncidentController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def incidentService

    def index() {
        incidentService.serviceMethod()
        redirect(action: "list", params: params)
    }

    // Other stuff
}

My service consists simply of this:

class IncidentService {

def grailsApplication

    def serviceMethod() {
        System.out.println("Grails application: " + grailsApplication)
    }
}

And I declare the injection of IncidentService in my resources.groovy as follows:

beans = {
    incidentService(org.myorg.test.IncidentService)
}

Now everything I have read online says that this should work fine and I should be able to access my configs through the grailsApplication in the service. However, grailsApplication is null whenever I try to. If I add a ConfigObject to the service and set it by passing grailsApplication.getConfig() from the controller, it works fine.

Can anyone please point me in the right direction for figuring out what the problem is here?

Thank you

Upvotes: 2

Views: 1380

Answers (1)

Gregg
Gregg

Reputation: 35904

You shouldn't have to add your IncidentService to resources.groovy. If that service is under grails-app/services it will be done for you. Remove that and try it again. I think that might be your problem

Upvotes: 3

Related Questions