【问题标题】:Can't inject grailsApplication into service无法将 grailsApplication 注入服务
【发布时间】:2012-07-31 15:54:50
【问题描述】:

我在将 grailsApplication 注入我在 Grails 2.1 中创建的服务时遇到问题。我已经构建了一个简单的测试应用程序(希望我能找出这个问题,然后将修复程序移植到我的真实应用程序中),它由一个控制器、视图和服务组成。

控制器如下所示:

class IncidentController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def incidentService

    def index() {
        incidentService.serviceMethod()
        redirect(action: "list", params: params)
    }

    // Other stuff
}

我的服务很简单:

class IncidentService {

def grailsApplication

    def serviceMethod() {
        System.out.println("Grails application: " + grailsApplication)
    }
}

并且我在我的 resources.groovy 中声明了 IncidentService 的注入,如下所示:

beans = {
    incidentService(org.myorg.test.IncidentService)
}

现在我在网上阅读的所有内容都表明这应该可以正常工作,并且我应该能够通过服务中的 grailsApplication 访问我的配置。但是,每当我尝试时, grailsApplication 都是空的。如果我将ConfigObject 添加到服务并通过从控制器传递grailsApplication.getConfig() 来设置它,它工作正常。

谁能指出我正确的方向以找出问题所在?

谢谢

【问题讨论】:

  • 您不必将 IncidentService 添加到 resources.groovy。如果该服务在 grails-app/services 下,它将为您完成。删除它,然后再试一次。我认为这可能是你的问题。
  • 原来如此。感谢您的帮助 Gregg,您是救生员!
  • 将其添加为答案,以便您接受。很高兴我能帮上忙。

标签: grails dependency-injection


【解决方案1】:

您不必将 IncidentService 添加到 resources.groovy。如果该服务在 grails-app/services 下,它将为您完成。删除它,然后再试一次。我想这可能是你的问题

【讨论】:

    猜你喜欢
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2019-06-03
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多