如何使用中static
定义的值初始化变量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}")
}
}
有什么线索吗?