为什么域类静态方法不能在 grails“服务”内部工作?
我希望 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
元类方法是在配置 Spring 应用程序上下文后连接的,因此尝试在 afterPropertiesSet 中调用它们将会失败。相反,您可以创建一个常规的 init() 方法并从 BootStrap: 调用它,
然后使用以下命令调用它:
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:
and call that with this:
我发现,真正的答案是不要这样做。
我应该将我的服务注入到我的域类中并从那里调用它。
我可以使用“触发”方法,例如 afterInsert 来根据需要调用我的服务方法
(例如,来自 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
(for example, from the grails services documentation)