我有一個問題,將grailsApplication注入到我在Grails 2.1中創建的服務中。我已經構建了一個簡單的測試應用程序(希望能夠解決此問題,然後將修復程序移植到我的實際應用程序中),該應用程序由單個控制器,視圖和服務組成。無法將grailsApplication注入服務
控制器看起來是這樣的:
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)
}
}
而且我宣佈IncidentService的注入在我resources.groovy如下:
beans = {
incidentService(org.myorg.test.IncidentService)
}
現在我在網上閱讀的一切都表示,這應該工作得很好,我應該b能夠通過服務中的grailsApplication訪問我的配置。但是,每當我嘗試時,grailsApplication都是null。如果我將ConfigObject
添加到服務中,並通過從控制器傳遞grailsApplication.getConfig()
來設置它,它工作正常。
任何人都可以請指出我在正確的方向找出問題是在這裏?
謝謝
您不應該將您的IncidentService添加到resources.groovy。如果該服務在grails-app/services下,則會爲您完成。刪除它,然後再試一次。我認爲這可能是你的問題。 – Gregg 2012-07-31 16:09:05
就是這樣。謝謝你的幫助Gregg,你是一個拯救生命的人! – 2012-07-31 16:13:32
補充說,作爲答案,你可以接受。很高興我能幫上忙。 – Gregg 2012-07-31 18:03:55