2012-04-25 78 views
1

我試過多個應用程序重新啓動,並從2.0.0升級到2.0.3,沒有幫助。我總是得到這個消息:服務沒有被注入到Grails域的類,Grails 2.0.0和2.0.3

: def getCustomerName(){64:  customerService.name 
"Cannot get property 'name' on null object" 

相關領域類部分:

class Instance { 
    def customerService 
    ... 

    def getCustomerName(){ 
     customerService.name 
    } 

相關的服務類:

class CustomerService { 

我試圖從其中有一個視圖訪問getCustomerName方法訪問實例的對象。也嘗試過與其他服務一樣的結果。

+0

是在'的grails-app/services'子目錄服務類的源文件? – ataylor 2012-04-25 18:23:48

+0

是的。我還應該提到CustomerService注入在Controller中工作,而不是在Domain對象中。 – 2012-04-25 19:07:18

+0

你有沒有嘗試做一個grails乾淨,然後運行你的應用程序。如果它在控制器中工作,它應該,不能想到其他任何東西。 – allthenutsandbolts 2012-04-25 19:27:37

回答

3

這裏只是一個猜測 - 以下讀起來就像一個getter方法:

def getCustomerName() 

是否有可能在框架的依賴注入發生之前檢查的有效性的getter和setter方法?

幾年前,Burt回答了一個關於在onLoad()中使用依賴注入的問題,它似乎就是這樣發生的。他建議:

添加此導入:

import org.codehaus.groovy.grails.commons.ApplicationHolder as AH 

然後引用客戶服務爲

def customerService = AH.application.mainContext.customerService 
+0

更名爲「測試」的方法,結果相同。幸運的是(不幸的是)你的建議恢復到手動注入工作,所以我想我會暫時接受AH棄用警告。這看起來像是一個Grails bug,想我應該報告它? – 2012-04-29 20:35:25

+0

它對我來說當然看起來像一個bug。 – Kelly 2012-04-29 22:10:25