简体   繁体   中英

Grails service class cross ref

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM