依赖注入不使用邮件插件在grails作业中工作

时间:2014-12-05 11:36:17

标签: grails gorm grails-2.0

我创建了一个服务 NotifierService来使用grails邮件插件发送电子邮件。

class NotifierService {
    MailService mailService

    def sendWarningEmail(String name, String email, Date blockingDate) {
        try {
            mailService.sendMail {
                to email
                from "noreply-myApp@domain.com"
                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", "user@domain.com", new Date())
    }
}

我正在尝试在grails作业notifyUser()

中调用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依赖注入。同样,问题与notifierService中的ApplicationUtilService相同,并通过实例化与上述相同来解决。

现在,真正的问题在于mailService。如上所述的实例化并不起作用。我怎么解决呢?

1 个答案:

答案 0 :(得分:1)

Null指针正在UpdateJob.applicationUtilService上发生,因此似乎没有正确地将applicationUtilService注入到UpdateJob中。 UpdateJob是否在正确的包中?