Grails - 无法从控制器调用服务 -->总是收到“无法调用空对象错误的方法”
我有一个 grails 应用程序,我正在按照此处的教程进行操作:
http://www.grails.org/Services
我有一些代码,例如
import org.springframework.beans.factory.InitializingBean
class SubmitRequestService implements InitializingBean{
def GrailsApplication1
def setting
void afterPropertiesSet(){
this.setting = GrailsApplication1.config.setting
}
def void sendHistoricalContract(HistoricalContract hc_instance){
//... blah blah whatever code
}
}
class SubmitRequestController {
def submitRequestService
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def index = {
// .... blah blah whatever code
submitRequestService.sendHistoricalContract(historicalContractInstance)
}
}
“不,无论我做什么,我都可以”似乎没有将服务注入到控制器中。每当我到达调用服务的行时,我都会收到错误:
错误 error.GrailsExceptionResolver - 无法在 null 对象上调用方法 sendHistoricalContract()
我做错了什么?
提前致谢
i have a grails application and i'm following the tutorial here:
http://www.grails.org/Services
i have some code like
import org.springframework.beans.factory.InitializingBean
class SubmitRequestService implements InitializingBean{
def GrailsApplication1
def setting
void afterPropertiesSet(){
this.setting = GrailsApplication1.config.setting
}
def void sendHistoricalContract(HistoricalContract hc_instance){
//... blah blah whatever code
}
}
class SubmitRequestController {
def submitRequestService
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def index = {
// .... blah blah whatever code
submitRequestService.sendHistoricalContract(historicalContractInstance)
}
}
No whatever i do, i can't seem to get the service to be injected into the controller. Whenever I get to the line where i call the service i get the error:
ERROR errors.GrailsExceptionResolver - Cannot invoke method sendHistoricalContract() on null object
What am i doing wrong?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GrailsApplication1 看起来很奇怪——它来自哪里?如果您想访问 GrailsApplication 实例来获取配置,请对
grailsApplication
Spring bean 使用依赖项注入:我的猜测是 GrailsApplication1 导致服务出现问题,导致其无法被注入控制器,但我认为它会在启动时出现异常而爆炸。尝试运行
grails clean
来强制完全重新编译。GrailsApplication1 looks weird - what's that coming from? If you want to access the GrailsApplication instance to get to the config, use a dependency injection for the
grailsApplication
Spring bean:My guess is that GrailsApplication1 is causing a problem with the service that's keeping it from being injected into the controller, but I would think that it'd blow up with an exception at startup. Try running
grails clean
to force a full recompile.我以前遇到过这个。
我将其更改为 grailsApplication1 并且它起作用了。
然后你调用:
注意服务的情况
I've run into this before.
I change it to
grailsApplication1
and it worked.Then you call:
Notice the case of the service
伯特的回答很有帮助(给伯特+1),但以防其他人遵循此处的教程:
http://www.grails.org/Services
并遇到了与我相同的问题,我想明确说明:
请参阅上面 Burt 对服务和 Spring 框架的其他资源的评论。
Burt's answer is helpful (+1 to Burt) but in case others are following the tutorial here:
http://www.grails.org/Services
and experiencing the same issue i had, i want to make it explicit:
See Burt's comment above on additional resources to Services and the Spring Framework.