在我的Serivce中尝试以下内容时,我遇到了一个问题:
class SendingMailService {
def dataSource // the Spring-Bean "dataSource" is auto-injected
def sendXLSMail() {
def db = new Sql(dataSource)
//additional code such as query, execution follows
}
}当我执行这段代码时,我会得到一个错误:“必须指定一个非空连接”。根据我所读到的,我认为上面的代码应该工作(这不是一个单元测试)...so,很明显,我遗漏了一些关于Grails的东西?
谢谢你的帮助,
发布于 2014-01-14 15:58:38
好的,几个月后,我有一些时间来研究这个问题。
为了让dataSource自动注入到服务中,我尝试了几种方法,但是我一直得到一个空连接。最后,我从控制器中传递数据源,这不是很优雅,但至少可以工作。我希望这能帮助其他人,因为我已经看到了一些类似的问题。仍然想知道为什么自动注入不能工作,但我想我必须深入了解Grails才能理解这一点。
class MyController {
def dataSource
def someControllerMethod() {
MyService myService = new MyService()
myService.serviceMethodQuery(dataSource)
}
}/
class MyService {
def serviceMethodQuery(Object dataSource){
//use your datasource as you normally would
}
}https://stackoverflow.com/questions/18491144
复制相似问题