Grails - 无法从控制器调用服务 -->总是收到“无法调用空对象错误的方法”

发布于 2024-10-09 08:46:12 字数 1048 浏览 5 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

伪心 2024-10-16 08:46:12

GrailsApplication1 看起来很奇怪——它来自哪里?如果您想访问 GrailsApplication 实例来获取配置,请对 grailsApplication Spring bean 使用依赖项注入:

class SubmitRequestService implements InitializingBean {

   private setting

   def grailsApplication

   void afterPropertiesSet() {
       setting = grailsApplication.config.setting
   }

   void sendHistoricalContract(HistoricalContract hc_instance) {
      //... blah blah whatever code      
   }
}

我的猜测是 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:

class SubmitRequestService implements InitializingBean {

   private setting

   def grailsApplication

   void afterPropertiesSet() {
       setting = grailsApplication.config.setting
   }

   void sendHistoricalContract(HistoricalContract hc_instance) {
      //... blah blah whatever code      
   }
}

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.

烧了回忆取暖 2024-10-16 08:46:12

我以前遇到过这个。

我将其更改为 grailsApplication1 并且它起作用了。

然后你调用:

this.setting = grailsApplication1.config.setting

注意服务的情况

I've run into this before.

I change it to grailsApplication1 and it worked.

Then you call:

this.setting = grailsApplication1.config.setting

Notice the case of the service

回梦 2024-10-16 08:46:12

伯特的回答很有帮助(给伯特+1),但以防其他人遵循此处的教程:
http://www.grails.org/Services
并遇到了与我相同的问题,我想明确说明:

服务位于其自己的文件中
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:

Services go in their own files under
the Services directory, you do not
combine them with controllers, even
though it looks that way in the code
examples

See Burt's comment above on additional resources to Services and the Spring Framework.

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