5
如何初始化一個static
變量,其值爲config.groovy
中定義的值?Grails:使用config.groovy中定義的值初始化一個靜態變量
目前,我有這樣的事情:
class ApiService {
JSON get(String path) {
def http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
...
}
JSON get(String path, String token) {
def http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
...
}
...
JSON post(String path, String token) {
def http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
...
}
}
我不想定義每個方法裏面http
變量(幾個GET,POST,PUT和DELETE)。
我想在該服務中將http
變量作爲static
變量。
我想這沒有成功:
class ApiService {
static grailsApplication
static http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
JSON get(String path) {
http.get(...)
...
}
}
我得到Cannot get property 'config' on null object
。同樣有:
class ApiService {
def grailsApplication
static http
ApiService() {
super()
http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
}
JSON get(String path) {
http.get(...)
...
}
}
我也試過沒有static
定義,但同樣的錯誤Cannot get property 'config' on null object
:
class ApiService {
def grailsApplication
def http
ApiService() {
super()
http = new HTTPBuilder("${grailsApplication.config.grails.api.server.url}")
}
}
任何線索?
謝謝伊恩!奇蹟般有效 :) – Agorreca