Gideon
Gideon

Reputation: 1537

Grails does not resolve same-named Services as expected

I have two services with the same name in my Grails 2.4.4 project, called RemittanceService. One is in the package on ph.bank (which I created weeks ago) and another is on ph.gov.advice.slip(which I created today). Since now there are two instances of the service with the same name, I replaced all dependency-injection of ph.bank.RemittanceService from:

class ... {
    def remittanceService

    ...
}

into this:

class ... {
    def bankRemittanceService
    // added the word `bank` from its package

    ...
}

and the injections for ph.gov.advice.slip.RemittanceService into:

class ... {
    def slipRemittanceService
    // added the word `slip` from its package

    ...
}

Now the problem is that it doesn't point the respective Service, and instead, returns an error:

java.lang.NullPointerException: Cannot invoke method [...] on null object

I decided to revert it my previous code. When I return the declaration into:

def remittanceService

it always points to the Service found on ph.bank, never to my newly-created Service. My current fix is:

import ph.gov.advice.slip.RemittanceService

class ... {
    def slipRemittanceService = new RemittanceService()
    // Here I initialize it instead on how dependencies should be declared

    ...
}

Although, I felt that is wrong. Is there more Grails-ly way to do this?

Upvotes: 0

Views: 328

Answers (1)

Taras Kohut
Taras Kohut

Reputation: 2555

You can declare beans in your resources.groovy file.

beans = {
  bankRemittanceService(ph.bank.RemittanceService) {
  }
  slipRemittanceService(ph.gov.advice.slip.RemittanceService) {
  }
}

now you can inject bankRemittanceService and slipRemittanceService

Upvotes: 2

Related Questions