我的Grails应用程序中有一个服务.但是我需要在我的应用程序中找到配置以进行某些配置.但是,当我尝试def grailsApplication在我的服务中使用时,它仍然为空.
我的服务是在"服务"下.
class RelationService {
def grailsApplication
private String XML_DATE_FORMAT = "yyyy-MM-dd"
private String token = 'hej123'
private String tokenName
String WebserviceHost = 'xxx'
def getRequest(end_url) {
// Set token and tokenName and call communicationsUtil
setToken();
ComObject cu = new ComObject(tokenName)
// Set string and get the xml data
String url_string = "http://" + WebserviceHost + end_url
URL url = new URL(url_string)
def xml = cu.performGet(url, token)
return xml
}
private def setToken() {
tokenName = grailsApplication.config.authentication.header.name.toString()
try {
token = RequestUtil.getCookie(grailsApplication.config.authentication.cookie.token).toString()
}
catch (NoClassDefFoundError e) {
println "Could not set token, runs on default instead.. " + e.getMessage()
}
if(grailsApplication.config.webservice_host[GrailsUtil.environment].toString() != '[:]')
WebserviceHost = grailsApplication.config.webservice_host[GrailsUtil.environment].toString()
}
}
Run Code Online (Sandbox Code Playgroud)
我已经将Inject grails应用程序配置看作服务,但它并没有给我一个答案,因为一切看起来都是正确的.
但是,我这样称呼我的服务: def xml = new RelationService().getRequest(url)
编辑:
忘了输入我的错误,这是: Cannot get property 'config' on null object
您的服务是正确的,但您调用它的方式不是:
def xml = new RelationService().getRequest(url)
Run Code Online (Sandbox Code Playgroud)
因为您正在“手动”实例化一个新对象,所以您实际上绕过了 Spring 进行的注入,因此“grailsApplication”对象为 null。
您需要做的是使用 Spring 注入您的服务,如下所示:
class MyController{
def relationService
def home(){
def xml = relationService.getRequest(...)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3843 次 |
| 最近记录: |