Grails服务类交叉引用 [英] Grails service class cross ref

查看:93
本文介绍了Grails服务类交叉引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到Grails服务类是Spring管理的单例。我也知道你可以通过在camel case中声明一个带有serviceClassName的本地def来引用另一个服务类。让我感到意外的是,我似乎无法像引用服务类一样交叉引用。

  class FirstService {

def secondService
...
}

class SecondService {

def firstService
...
}

对于每个人来说这是真的吗,还是我在...部分的某个地方弄得一团糟? p>

解决方案

当有循环引用时,Grails无法注入。实际上,您应该收到行的异常。FactoryBean尚未完全初始化。有一个关于这个问题的JIRA问题,他们表示他们不会解决这个问题,因为它比Spring更像是使用Spring而不是Grails( http://jira.grails.org/browse/GRAILS-5080

然而,有一个解决方法是在JIRA和我可以验证是否适用于Grails 2.0.RC1。在SecondService中,使它< protected def firstService 并在其下添加 def grailsApplication ,然后添加方法 def initialize(){this.firstService = grailsApplication.mainContext.firstService} 。最后,在BootStrap.groovy中添加 def firstService ,然后在 init 闭包中添加 secondService.initialize()。不是一个漂亮的解决方案,但这会让所有的东西都以你想要的方式联结起来。


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.

这篇关于Grails服务类交叉引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆