Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails service class cross ref

Tags:

grails

service

I realize that Grails service classes are Spring managed singletons. I also know that you can reference one service class from another by just declaring a local def with the serviceClassName in camel case like that. What surprised me was that I can't seem to cross reference service classes together like so

 class FirstService {

   def secondService
 ...
 }

 class SecondService {

   def firstService
 ...
 }

Is this true for everyone, or did I mess up somewhere in the ... section?

like image 662
dbrin Avatar asked Nov 03 '11 22:11

dbrin


1 Answers

Grails isn't able to inject when there are circular references. You should actually be getting an exception along the lines of FactoryBean is not fully initialized yet. There's a JIRA issue about this where they stated they won't be fixing this, as it's more to do with Spring than Grails (http://jira.grails.org/browse/GRAILS-5080)

However, there is a workaround that is cited in the JIRA and I can verify does work with Grails 2.0.RC1. In SecondService, make it protected def firstService and add def grailsApplication below that and then add the method def initialize() { this.firstService = grailsApplication.mainContext.firstService }. Lastly, in BootStrap.groovy, add def firstService and then in the init closure, add secondService.initialize(). Not a pretty solution, but this will get everything hooked up the way you want it.

like image 187
Todd Avatar answered Nov 11 '22 06:11

Todd