Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails service using a method from another service

Tags:

grails

service

I'm building a grails app and have come across an issue when trying to instatiate a service, in a different service. Both of the services use a method defined in the other eg.

class fooService{
    def barService
    barService.doIt()

    def getIt(){
    ...
    }
}

class barService{
    def fooService
    fooService.getIt()

    def doIt(){
    ...
    }
}

When I run the app and go to where the methods are used, it brings up this error;

Error creating bean with name 'fooService': 
org.springframework.beans.factory.FactoryBeanNotInitializedException: FactoryBean is 
not fully initialized yet

Is this something that cannot be done in grails? Or could anyone offer any advice?

Thanks

like image 989
Dan Fox Avatar asked Aug 31 '12 08:08

Dan Fox


1 Answers

I've had similar issues in the past, but only when both services are transactional. If it's possible to make at least one of them non-transactional then it should work as-is. If that's not possible then the fallback is to do a kind of "late binding"

class FooService {
  def grailsApplication
  private getBarService() {
    grailsApplication.mainContext.barService
  }

  public methodThatUsesBarService() {
    barService.doSomething()
  }
}

This will look up barService in the app context at the point where it is used, rather than at the point where the FooService is created.

like image 193
Ian Roberts Avatar answered Sep 28 '22 06:09

Ian Roberts