Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails dependency injection via autowiring doesn't work

I have a Service, called Mailer, and a controller, which should use this service:

class DocumentController {

    def mailer

    def publish = {
        mailer.sendReport()
    }

}

But when I call publish I get:

java.lang.NullPointerException: Cannot invoke method sendReport() on null object

Why doesn't the dependency injection work here?

like image 441
deamon Avatar asked Feb 26 '23 08:02

deamon


1 Answers

Grails is all about conventions. I think mailer needs to be called mailerService. It needs to be in the services directory. Controller needs to be in the controllers directory.

From the documentation

"A service contains business logic that can be re-used across a Grails application. In Grails a service is a class that ends in the convention "Service" and lives in the grails-app/services directory. A service can be created with the create-service command:"

like image 169
hvgotcodes Avatar answered Mar 08 '23 07:03

hvgotcodes