通过自动装配的 Grails 依赖注入不起作用
我有一个名为 Mailer
的服务,以及一个应该使用此服务的控制器:
class DocumentController {
def mailer
def publish = {
mailer.sendReport()
}
}
但是当我调用 publish
时,我得到:
java.lang.NullPointerException: Cannot invoke method sendReport() on null object
为什么依赖项注入在这里不起作用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Grails 就是关于约定的。我认为mailer需要被称为mailerService。它需要位于 services 目录中。控制器需要位于控制器目录中。
从文档
“服务包含可以在 Grails 应用程序中重用的业务逻辑。在 Grails 中,服务是一个以约定“Service”结尾的类,位于 grails-app/services 目录中。服务可以是使用 create-service 命令创建:”
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:"