Grails service using a method from another service

I am creating a grails application and am encountering a problem when trying to install a service in another service. Both services use a method defined in another, for example.

class fooService{ def barService barService.doIt() def getIt(){ ... } } class barService{ def fooService fooService.getIt() def doIt(){ ... } } 

When I launch the application and move on to where the methods are used, it causes this error:

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

Is it impossible to do in the grail? Or can anyone offer any advice?

thanks

+4
source share
5 answers

I had similar problems in the past, but only when both services are transactional. If it is possible to make at least one of them non-transactional, then it should work as is. If this is not possible, the reserve should make a kind of "late binding"

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

It will look like barService in the context of the application in the place where it is used, and not at the point where the FooService created.

+4
source

A service may be called by another service, but not possible during initialization. If you want to implement this, the path should be like this.

 class fooService{ def barService def getIt(){ ... } def anotherFooMethod(){ barService.doIt(); } } class barService{ def fooService def doIt(){ ... } def anotherBarMethod(){ fooService.getIt(); } } 
+3
source

All of these answers are excellent and show how to deal with this problem through structure. Although, when I had this problem, I realized that there should be a flaw in my plan and architecture, if I absolutely must have services that cause each other and cause conflicts. Instead of finding a job, I took a somewhat less complex and lasting approach - I restructured. I have moved violation methods from a service class to another service class. It took some rethinking, rethinking and copying / pasting skills, but I think the application is better for him.

I am not saying this is better than the other answers. I say this time, when this refactoring project was a better, faster and less complicated solution. I highly recommend it.

UPDATE Our final strategy is to reorganize all the utility functions of the "utility" into the baseService class, and then all other services that need auxiliary services extend the baseService. The new policy is to not inject injection services into other services unless there is a situation that prevents us from following this pattern of inheritance. This will give us a cleaner code base and less spaghetti footprint of injections. In addition, this eliminates the occurrence of this error.

+2
source

This is not valid code, so it’s hard to understand what is really happening. Are doIt() and getIt() calls constructors? If so, change the services to the implementation of org.springframework.beans.factory.InitializingBean and make calls in the afterPropertiesSet method.

+1
source

You can handle a circular reference using the following:

Lets call it firstSerivce and secondService.

Code changes for the secondService class

 protected def firstService def grailsApplication def initialize() { this.firstService = grailsApplication.mainContext.firstService } 

code changes in Bootstrap.groovy

 def secondService def init = { servletContext -> secondService.initialize() ... .. 
0
source

All Articles