ApplicationUtilService

ApplicationUtilService

我创建了一个服务NotifierService以使用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())
    }
}

我正在尝试在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依赖注入(inject)。再次,notifierService中的ApplicationUtilService存在相同的问题,并通过实例化与上面相同的方法进行了修复。

现在,真正的问题是mailService。上面的实例无效。我该如何解决

最佳答案

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

07-27 13:33