为什么域类静态方法不能在 grails“服务”内部工作?

发布于 2024-08-26 18:45:09 字数 1053 浏览 4 评论 0原文

我希望 grails 服务能够访问域静态方法以进行查询等。

例如,在控制器中,我可以调用

IncomingCall.count()

以获取表“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: []

这些方法如何注入?控制器中没有神奇的 def 语句可以做到这一点。或者问题是 Hibernate 在我的 Service 类中不可用?

我也尝试过这种方法:

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
}

但失败了。 ApplicationHolder.application.getClassForName("IncomingCall") 返回 null。现在这样称呼是否还为时过早?是否有可以调用的“late init”?我认为这就是“afterPropertiesSet()”的目的......

I want a grails service to be able to access Domain static methods, for queries, etc.

For example, in a controller, I can call

IncomingCall.count()

to get the number of records in table "IncomingCall"

but if I try to do this from inside a service, I get the error:

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: []

How do these methods get injected? There's no magic def statement in a controller that appears to do this. Or is the problem that Hibernate isn't available from my Service class?

I also tried it this way:

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
}

and it failed. ApplicationHolder.application.getClassForName("IncomingCall") returned null. Is this just too early to call this? Is there a "late init" that can be called? I thought that was the purpose of "afterPropertiesSet()"...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

躲猫猫 2024-09-02 18:45:09

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

import ms.wdw.tropocontrol.IncomingCall

class FooService {

   void init() {
      int count = IncomingCall.count()
      println "Count is " + count
   }
}

然后使用以下命令调用它:

class BootStrap {

   def fooService

   def init = { servletContext ->
      fooService.init()
   }
}

The metaclass methods are wired up after the Spring application context is configured, so trying to call them in afterPropertiesSet will fail. Instead you can create a regular init() method and call that from BootStrap:

import ms.wdw.tropocontrol.IncomingCall

class FooService {

   void init() {
      int count = IncomingCall.count()
      println "Count is " + count
   }
}

and call that with this:

class BootStrap {

   def fooService

   def init = { servletContext ->
      fooService.init()
   }
}
聽兲甴掵 2024-09-02 18:45:09

我发现,真正的答案是不要这样做。

我应该将我的服务注入到我的域类中并从那里调用它。

我可以使用“触发”方法,例如 afterInsert 来根据需要调用我的服务方法

class Deal {
    def authenticateService

    def afterInsert() {
        def user = authenticateService.userDomain();
        ....
    }
....
}

(例如,来自 grails 服务文档)

The real answer, I discovered, is not to do this.

I should instead inject my service into my domain class and call it from there.

I can use the "trigger" methods, like afterInsert to call my service methods as needed

class Deal {
    def authenticateService

    def afterInsert() {
        def user = authenticateService.userDomain();
        ....
    }
....
}

(for example, from the grails services documentation)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文