我已经创建了一个服务NotifierService
使用grails邮件插件发送电子邮件。依赖注入不能在使用邮件插件的grails作业中工作
class NotifierService {
MailService mailService
def sendWarningEmail(String name, String email, Date blockingDate) {
try {
mailService.sendMail {
to email
from "[email protected]"
subject "Status message: Warning"
body (view:"/email/warningEmail", model:[
name:name,
blockingDate:blockingDate
])
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
我创造了另一种服务ApplicationUtilService
在我试图使用NotifierService
。
class ApplicationUtilService{
def notifierService
def notifyUser(){
notifierService.sendWarningEmail("User name", "[email protected]", new Date())
}
}
我试图打电话notifyUser()
在Grails的工作UpdateJob
class UpdateJob{
def applicationUtilService
static triggers = {
// Scehduling parameters
}
def execute(){
applicationUtilityService.notifyUser()
}
}
我收到以下错误
Error 2014-12-05 12:04:53,550 [quartzScheduler_Worker-1] ERROR listeners.ExceptionPrinterJobListener - Exception occurred in job: Grails Job
Message: java.lang.NullPointerException: Cannot invoke method notifyUser() on null object
Line | Method
->> 111 | execute in grails.plugins.quartz.GrailsJobFactory$GrailsJob
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 202 | run in org.quartz.core.JobRunShell
^ 573 | run . . in org.quartz.simpl.SimpleThreadPool$WorkerThread ...`
它的工作原理,当我实例为
ApplicationUtilService applicationUtilService = new ApplicationUtilService()
而不是使用grails依赖注入。再次,ApplicationUtilService
中的notifierService
也出现同样的问题,并通过实例化上述相同的问题进行修复。
现在,真正的问题是与mailService
。上面的实例化不起作用。我怎么能解决它
你有没有在'grails-app/services'下定义你的两个服务? – injecteer 2014-12-05 12:28:06
是的,在服务 – Syam 2014-12-05 12:30:51
下定义我忘记提及我正在使用grails 2.2.4版本 – Syam 2014-12-05 12:36:53