为什么Domain类静态方法不能在grails"服务"中工作?

1 dns grails groovy hibernate

我希望grails服务能够访问域静态方法,查询等.

例如,在控制器中,我可以打电话

IncomingCall.count()
Run Code Online (Sandbox Code Playgroud)

获取表"IncomingCall"中的记录数

但如果我尝试从服务中执行此操作,我会收到错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'incomingStatusService': Invocation of init method failed; nested exception is groovy.lang.MissingMethodException: No signature of method: static ms.wdw.tropocontrol.IncomingCall.count() is applicable for argument types: () values: []
Run Code Online (Sandbox Code Playgroud)

这些方法如何注入?在控制器中没有神奇的def语句似乎这样做.或者是我的Service类无法使用Hibernate的问题?

我也这样试过:

import ms.wdw.tropocontrol.IncomingCall
import org.codehaus.groovy.grails.commons.ApplicationHolder

// ...

void afterPropertiesSet() {

    def count = ApplicationHolder.application.getClassForName("IncomingCall").count()
    print "Count is " + count
}
Run Code Online (Sandbox Code Playgroud)

它失败了.ApplicationHolder.application.getClassForName("IncomingCall")返回null.现在称这个为时尚早?是否有可以调用的"晚期初始化"?我认为这是"afterPropertiesSet()"的目的......

Bur*_*ith 5

配置Spring应用程序上下文后,元类方法接线,因此尝试在afterPropertiesSet中调用它们将失败.相反,您可以创建一个常规的init()方法并从BootStrap调用它:

import ms.wdw.tropocontrol.IncomingCall

class FooService {

   void init() {
      int count = IncomingCall.count()
      println "Count is " + count
   }
}
Run Code Online (Sandbox Code Playgroud)

并称之为:

class BootStrap {

   def fooService

   def init = { servletContext ->
      fooService.init()
   }
}
Run Code Online (Sandbox Code Playgroud)